Very basic program but its making me crazy
First post , hello
I'm new to java and I'm trying to get this basic program working to find the shortest word in a string.
for some reason when it runs it will always declare the last word entered in the string as being the shortest word , regardless how many letters are in it or if its a word or an integer entered.
Can anyone see what im doing wrong?
Code Java:
String sentence;
String shortestword;
String word;
int spacePos;
shortestword = "";
System.out.print("Please enter a sentence: ");
sentence = getString();
do{
spacePos = sentence.indexOf(" ");
if (spacePos == -1)
{
word = sentence;
}
else
{
word = sentence.substring(0,spacePos);
sentence = sentence.substring(spacePos +1);
}
if(word.length() > shortestword.length())
{
shortestword = word;
}
}
while (spacePos != -1);
System.out.println("The shortest word is " + word)
Re: Very basic program but its making me crazy
When posting code, make sure you use the code or highlight tags to preserve formatting. Also, make sure it's in the form of an SSCCE (that's a link).
But have you tried putting in some print statements to figure out what's going on? Or better yet, have you tried running through this with a debugger?
Re: Very basic program but its making me crazy
Quote:
Originally Posted by
KevinWorkman
When posting code, make sure you use the code or highlight tags to preserve formatting. Also, make sure it's in the form of an
SSCCE (that's a link).
But have you tried putting in some print statements to figure out what's going on? Or better yet, have you tried running through this with a debugger?
hi Kevin
added the code tags
It compiles and runs fine in j-creator, it's just outputting the wrong answer and I cant for the life of me figure out why , this might sound naive but whats a debugger?
Re: Very basic program but its making me crazy
From my point of view, Debugger might be over the top for a beginner but that's not here or there.
Problems in your program are very small, and once you spot them, it will take seconds to adjust.
I recommend checking out:
Re: Very basic program but its making me crazy
thanks for the help ,ill go back in on it now , you would laugh if you knew how long i have been plugging away at this and i just know its something very simple :)
Re: Very basic program but its making me crazy
Deeply sorry, Posted advice without checking it on my own System! Although suggestions I pointed towards were needed, It was not a clear-cut (Quote: Few second Job) like I said.
Look into the split method of the String class:
String (Java 2 Platform SE v1.4.2)
By splitting up your sentence, and storing it into a String Array, you can easily go through the array comparing lengths with only a fraction of your code.
The parameter of split() is something you consider to be a good checkpoint to split the string, such as : or a whitespace.
Click here for details on for loops
Click here for an example of String.split() in action.
Again, very sorry for not checking my post before saying It was a 2 second job for you :P