Home arrow static arrow Java Programming [Archive] - Reading from a string.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Reading from a string.
This topic has 8 replies on 1 page.

Posts:6
Registered: 8/2/04
Reading from a string.  
Aug 2, 2004 3:38 AM



 
Sorry, I am only new to java, and am having some troubles.

Here is the probem i need a solution to:

I need to read a sentence from keyboard entry, i've done that, i need to convert all non-letters to a space, i've done that. The next part is the part im struggling with:

I have to read each word individually from the sentence so that i can sort them in aphabetical order and print the word from the sentence that would appear as first in order, and first in the dictionary, not printing any other words, just the first one.

So basically, I need code to read a sentence from a keyboard and displays the word that is alphabetically first, ie. the word that would appear first before any of the others in a dictionary(ignoring case).

Can anyone help on this topic? Any help would be greatly appreciated!
 

Posts:319
Registered: 11/8/00
Re: Reading from a string.  
Aug 2, 2004 3:46 AM (reply 1 of 8)



 
Take a look at the following API docs

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
and
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)
 

Posts:6
Registered: 8/2/04
Re: Reading from a string.  
Aug 2, 2004 3:55 AM (reply 2 of 8)



 
thanks for the links, but how would you suggest i implement them into my code so as though i am comparing the individual words from my string as to find which on is alphabetically superior? Here is my source code, if you could help,. i would appreciate this greatly.

import ktio.*;public class Sentence{	public static void main(String[] args)throws Exception	{		String sentence;		String processSentence = "";		String savedWord = "";		int currentChar = 0;		int sentenceLength;		char checkChar;		char startWord;		char endWord;							KeyboardReader kr = new KeyboardReader();				sentence = kr.getText("Enter a sentence: ");				sentenceLength = sentence.length();				for(currentChar = 0; currentChar < sentenceLength; currentChar++)		{			checkChar = sentence.charAt(currentChar);			/*if statement to check each character to see if they are a letter			if true they are added to processSentence			if false they are replaced by a space / whitespace			in processSentence */			if (Character.isLetter(checkChar))			{				processSentence += checkChar;			}			else				processSentence += " ";		}				//System Print Line : Final print out.		//The sentence isn't needed in the final print		//only the word that is first in order		//System.out.println(processSentence);			}}
 

Posts:6
Registered: 8/2/04
Re: Reading from a string.  
Aug 2, 2004 3:56 AM (reply 3 of 8)



 
some of the variables in my above code are actually useless at the moment, sorry if they are confusing!
 

Posts:319
Registered: 11/8/00
Re: Reading from a string.  
Aug 2, 2004 4:24 AM (reply 4 of 8)



 
you could have something like this, but i'm sure that there is a better way.

public static void main(String[] args)	{ 		int currentLength = 0;		String processSentence = "This is a sentance"; 		StringTokenizer st = new StringTokenizer(processSentence, " ", false);		String newString;		int totalTokens = st.countTokens();		String[] newList = new String[totalTokens];          for (int i = 0; i < totalTokens; i++)        {			int counter = 0;	        if (newList[counter] == null)	        {		        newList[counter] = st.nextToken();		        currentLength++;		        continue;	        } 	        newString = st.nextToken(); 			// find required location to insert object			while ((counter < currentLength) &&				   (newList[counter].compareToIgnoreCase(newString) < 0))			{				counter++;			} 			// At this point we have located required position			for (int j = currentLength; j > counter; j--)			{				newList[j] = newList[j - 1];			}	        newList[counter] = newString;	        currentLength++;        }                 // Test the result		for (int k = 0; k < newList.length; k++)		{			System.out.println("k = " + newList[k]);		}	}
 

Posts:6
Registered: 8/2/04
Re: Reading from a string.  
Aug 2, 2004 4:34 AM (reply 5 of 8)



 
thanks njadams but the StringTokenizer doesnt want to work, i've got it recognising the words individually now, i just need to work out the way to compare them and print the alphabetically correct word, this is the code i am using, what would help to put the entry in alphabetical order? ::

String[] result = processSentence.split("\\s");     		for (int x = 0; x < result.length; x++)     		thisWord = result[x];     		if (thisWord.compareTo(savedWord) > 0)     		{     			savedWord = thisWord;     		}     		else     		{     			savedWord = savedWord;     		}        	System.out.println("'" + savedWord + "' would appear first in the dictionary");


any ideas or any corrections?
 

Posts:6
Registered: 8/2/04
Re: Reading from a string.  
Aug 2, 2004 4:39 AM (reply 6 of 8)



 
The last code, no matter what stores each new token in the [code]savedWord[/word] String, no matter whether it be greater than or less than! does anyone know how to fix my segment of code? I know im being annoying but I like to learn.
 

Posts:319
Registered: 11/8/00
Re: Reading from a string.  
Aug 2, 2004 5:43 AM (reply 7 of 8)



 
The compareTo takes account of case, use compareToIgnoreCase to ignore the case. I think that uppercase is before lowercase.
 

Posts:6
Registered: 8/2/04
Re: Reading from a string.  
Aug 2, 2004 5:48 AM (reply 8 of 8)



 
I've used the
compareToIgnoreCase()
but it still send the same results to the println, so i dont know, i keep trying just aint moving at the moment. thanks for the help!
 
This topic has 8 replies on 1 page.