How to print out the lexicographically smallest word

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4636 Views - Last Post: 07 September 2012 - 07:52 PM Rate Topic: -----

#1 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

How to print out the lexicographically smallest word

Posted 07 September 2012 - 02:00 PM

I have this assignment that requires me to complete six tasks the program will carry out. I've done all but the sixth task and am very lost. I've already gotten it written out so the user types in an input but the problem i'm having is writing the part of the program to solve the task. The task asks for a user input of a single integer n, followed by n words separated by spaces. Then the output will be the lexicographically smallest word. For example, the input is "4 wait what is this" and the output is the word "is". I'm pretty sure I need to compare the length of the words, or use compareTo() to solve this but don't know how or where to begin. Create a for loop? Below is my mixture of what I think i need but i know most of it is wrong. Btw, all of the code that is in "//" is stuff i temporarily took out of the program to test the code without errors. And i don't need to worry about errors in the input, i was told the input will be exactly as give, single integer n followed by n words.

So, the question I have or statement, is could someone please point me in the right direction or give me a good example of how to find the lexicographically smallest word without using arrays.

Thanks in advance, Chris


import java.util.Scanner;

public class StringFun {
	
	
	public static void main(String[] args) {

Scanner sc6 = new Scanner(System.in);
		int n = sc6.nextInt();
		for (int i=0; i<n; i++){
			for (int j=0; j<i; j++);
			
			String str1 = sc6.nextLine();
			int len = str1.length();
//for (int j=0; j<n; j++){
			
			//String str2 = sc6.nextLine();
			//int len2 = str2.length();
			
			//if (len < len2)
				//System.out.println("len < len2");
			
			//else if (len > len2)
				//System.out.println("len > len2");
		//}
			System.out.println(len);
		}
sc6.close();
}






Is This A Good Question/Topic? 0
  • +

Replies To: How to print out the lexicographically smallest word

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 03:25 PM

if you want to read word by word use next() not nextLine()
actually the number of words is completly useless if all the words are on the same line

String line = scan.nextLine();
String[] words = line.split(" ");
now you can llop starting with 1 (to ignore the number of words) to words.length
saving which words[] is the smallest
Was This Post Helpful? 1
  • +
  • -

#3 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 03:52 PM

View Postpbl, on 07 September 2012 - 03:25 PM, said:

if you want to read word by word use next() not nextLine()
actually the number of words is completly useless if all the words are on the same line

String line = scan.nextLine();
String[] words = line.split(" ");
now you can llop starting with 1 (to ignore the number of words) to words.length
saving which words[] is the smallest



Thank you for your response.
I'll work with what you said, still not sure how to print out the smallest word from inside the String[] words. Is there a command to do that? I'm pretty new to java so my understanding of it isnt that great yet.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 04:00 PM

deepdivin247: Strings are Comparable, which means calling String.compareTo(String), will return the (lexicographically) smaller of the two. Using that method as a foundation, Arrays.sort, will sort any Comparable set of objects.
Was This Post Helpful? 1
  • +
  • -

#5 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 04:15 PM

View Postblackcompe, on 07 September 2012 - 04:00 PM, said:

deepdivin247: Strings are Comparable, which means calling String.compareTo(String), will return the (lexicographically) smaller of the two. Using that method as a foundation, Arrays.sort, will sort any Comparable set of objects.


Okay I understand that. What i don't know how to do or what i just cant visualize is that for my program, i don't know how many strings the user will input each time. if it was a constant 4 or 5, i would be able to write the program to compare [0] with [3] and [1] with [4]. Am i saying that correctly? Do i use some variable "i" in place of strings and loop through to compare each? An int cannot be used to compare strings though....right?

Does sorting the words in the array put them in lexicographical order? And then from there I can print out the first word?

Scanner sc6 = new Scanner(System.in);
		int n = sc6.nextInt();
		for (int i=0; i<n; i++){
			
			String str1 = sc6.nextLine();
			String[] words = str1.split(" ");
			Arrays.sort(words);
	        System.out.println(Arrays.toString(words));
	        
	        //System.out.println(words [0]);
	        
			for (int j = 1; j<words.length; j++){
				//System.out.print(str1.length());
			}


Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 04:49 PM

Quote

Does sorting the words in the array put them in lexicographical order? And then from there I can print out the first word?


Yes. With that being said, how you want to construct the array is up to you. Generally, if you don't know the size of the input, you'd use a dynamic structure (e.g. List). You can sort it with Collections.sort or convert it to an array with List.toArray.

I can't give you concrete advice since I don't know what the constraints are. For instance, assuming this is an assignment, you may not be able to use dynamic data structures. If you must use an array, you can (1) assume an input size or (2) ask the user how many strings they'd like to enter. Another thing: Do you expect the user to enter one word per line? Those are things I'd rather not get involved with.
Was This Post Helpful? 1
  • +
  • -

#7 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 04:55 PM

View Postblackcompe, on 07 September 2012 - 04:49 PM, said:

Quote

Does sorting the words in the array put them in lexicographical order? And then from there I can print out the first word?


Yes. With that being said, how you want to construct the array is up to you. Generally, if you don't know the size of the input, you'd use a dynamic structure (e.g. List). You can sort it with Collections.sort or convert it to an array with List.toArray.

I can't give you concrete advice since I don't know what the constraints are. For instance, assuming this is an assignment, you may not be able to use dynamic data structures. If you must use an array, you can (1) assume an input size or (2) ask the user how many strings they'd like to enter. Another thing: Do you expect the user to enter one word per line? Those are things I'd rather not get involved with.


Haha, no not one word per line. An example would be,

Input:
4 wait what is this 


Output:
 is 


I cant see the output on my laptop, but in that output code box, it should have the word "is".

I'm actually not supposed to be using arrays since we technically havent learned them yet, but since ive taken a class in C, i sort of have an idea. So i can use them. Not sure what you mean by dynamic data structures so i'm gonna go with a no on using those. and the 4 in front of the words in my input above is where the user tells the program how many strings they'd like to enter....followed with the four words.

This post has been edited by deepdivin247: 07 September 2012 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 04:58 PM

No need to know how to sort... Java has all the required methods to do that
Was This Post Helpful? 1
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 05:03 PM

deepdivin247: You've got the code written already...

  • Get the input with a Scanner.
  • Split it into an array. Lose the first element (since it's an integer).
  • Sort the array and print the first element.


Lose the for loop in your code and lose the Scanner.nextInt. You don't need it. You don't even need that integer. There are many ways to do this. I'm giving you one.

This post has been edited by blackcompe: 07 September 2012 - 05:06 PM

Was This Post Helpful? 1
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 05:15 PM

You can also do this without using arrays.

  • Get the number of words with Scanner.nextInt.
  • In a loop, get each word with Scanner.next.
  • If the word is smaller than the smallest word, it becomes the smallest. Since the smallest word will initially be null, consider reading a single word before entering the loop.

Was This Post Helpful? 1
  • +
  • -

#11 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 05:24 PM

View Postblackcompe, on 07 September 2012 - 05:03 PM, said:

deepdivin247: You've got the code written already...

  • Get the input with a Scanner.
  • Split it into an array. Lose the first element (since it's an integer).
  • Sort the array and print the first element.


Lose the for loop in your code and lose the Scanner.nextInt. You don't need it. You don't even need that integer. There are many ways to do this. I'm giving you one.


Funny thing is, I was looking at Eclipse and going through my program testing it without certain stuff and this is what I came up with, before i read what you wrote.

import java.util.Scanner;
import java.util.Arrays;


public class delete {

	public static void main(String[] args) {
		
		Scanner sc6 = new Scanner(System.in);
			
			String str1 = sc6.nextLine();
			String[] words = str1.split(" ");
			Arrays.sort(words);
			System.out.println(words[1]);
	        System.out.println(Arrays.toString(words));
		
		sc6.close();
	}
}



Now, while this does work and i'm okay with leaving it as it is, but I dont think this is exactly what the assignment is asking. Because i do need that Scanner.nextInt so that it can read the user input.

On the other hand, and correct me if i'm wrong, but my program is scanning that Int and putting it into my array [0], and then my program goes through each word, sorts it, then prints out whatever word is in array[1]. Is that what's happening?

Isn't my code just ignoring the integer I put in the front?

View Postblackcompe, on 07 September 2012 - 05:15 PM, said:

You can also do this without using arrays.

  • Get the number of words with Scanner.nextInt.
  • In a loop, get each word with Scanner.next.
  • If the word is smaller than the smallest word, it becomes the smallest. Since the smallest word will initially be null, consider reading a single word before entering the loop.


I'm going to try and write the code out this way on the side to see if i can figure it out. It sounds like the correct way to go if i actually stay on the guidelines of the assignment.
Was This Post Helpful? 0
  • +
  • -

#12 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 05:28 PM

Quote

Now, while this does work and i'm okay with leaving it as it is, but I dont think this is exactly what the assignment is asking. Because i do need that Scanner.nextInt so that it can read the user input.


My last post is probably the way your teacher intended it to be written and it does make use of Scanner.nextInt, so I'd go with that.

Quote

On the other hand, and correct me if i'm wrong, but my program is scanning that Int and putting it into my array [0]


Yes, that's what's happening. It's ignoring that integer. Depending on how you write the code, that integer may or may not be necessary.
Was This Post Helpful? 1
  • +
  • -

#13 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 05:34 PM

View Postblackcompe, on 07 September 2012 - 05:28 PM, said:

Quote

Now, while this does work and i'm okay with leaving it as it is, but I dont think this is exactly what the assignment is asking. Because i do need that Scanner.nextInt so that it can read the user input.


My last post is probably the way your teacher intended it to be written and it does make use of Scanner.nextInt, so I'd go with that.

Quote

On the other hand, and correct me if i'm wrong, but my program is scanning that Int and putting it into my array [0]


Yes, that's what's happening. It's ignoring that integer. Depending on how you write the code, that integer may or may not be necessary.



So right now I have

import java.util.Scanner;

public class delete {

	public static void main(String[] args) {
		
		Scanner sc6 = new Scanner(System.in);
		int n = sc6.nextInt();
		String str1 = sc6.nextLine();
		for (int i=0; i<n; i++){
			
			String str2 = sc6.nextLine();
		}
			System.out.println(str1);
		
		sc6.close();
	}
}



Which i'm sure you can tell or figure out, it's taking the input number lets say 4 and looping through those 4 times. I guess i dont understand exactly what to do. then i have it set to print out what it received from the loop, just to have the visual.

With what you've said, I dont see how to compare the words to find the smallest one in the loop? i'm assuming java does that for us. But then how does it store those words and where?

This post has been edited by deepdivin247: 07 September 2012 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#14 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 06:12 PM

Firstly, you should be using Scanner.next to pick up one word, not the whole line. Second, you should then compare that word to the smallest you've seen so far. I'll leave that to you. Think about how you'd do it with integers.

small = getWord();
for i = 0 to n-1
     word = getWord()
     if(word < small)
         small = word
print(small)



You'll need to use String.compareTo to compare strings.
Was This Post Helpful? 0
  • +
  • -

#15 deepdivin247  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 20-April 12

Re: How to print out the lexicographically smallest word

Posted 07 September 2012 - 07:44 PM

Hey, thanks for all your help.
I ended up just turning in the assignment. Its due at 1159 tonight. I still want to figure it out but will do on my own time. Thank you so much for your help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2