problem in my while loop code
thanks for viewing my post! My code is supposed to as the user to input a sentence, than display if its odd or even number of words. (MY first problem is that if i write (if odd = true) it will always come up as odd, but if i write if(odd == true) it will come up as even always. I'm not sure what statements to carry out to make this work correctly.) also, the program is supposed to keep asking the question and displaying if its odd or even unless the user inputs "stop already!". my second problem is that it wont stop when i type that in. I can see that the problem is that somehow the program is registering each individual word, so it wont recognize Stop already!. i dont know what to set words to to fix this. (i think thats the problem)
Besides that, the only other thing is that it displays the questions and answers multiple times instead of just once: (ex.):
Enter a sentence or type 'Stop Already' to quit the program.
Make this work!
words read as: Make
That's even!
Enter a sentence or type 'Stop Already! to quit the program.
That's even!
Enter a sentence or type 'Stop Already! to quit the program.
That's even!
Enter a sentence or type 'Stop Already! to quit the program.
here is my tester class:
Code:
public class OddSentenceTester
{
public static void main(String[] args) throws IOException {
boolean odd;
String words = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence or type 'Stop Already' to quit the program.");
words = in.next();
System.out.println("words read as: " + words);
while (!words.equals("Stop Already!"))
{
OddSentence first = new OddSentence(words);
odd = first.isOdd();
if(odd == true)
System.out.println("That's odd!");
else
System.out.println("That's even!");
System.out.println("Enter a sentence or type 'Stop Already! to quit the program.");
words = in.next();
}
}
}
and here is the oddSentence codE:
Code:
public class OddSentence {
String input = "";
int num;
OddSentence(String line) {
input = line;
}
public boolean isOdd() {
num = input.split(" ").length;
if(num % 2 == 0){
return true;
}
else {
return false;
}
}
}
Thanks for the help i cannot figure this one out!
Re: problem in my while loop code
A single = is assignment. Use it when you want to set a variable equal to a value. Double == tests for equality. Use it when you want to compare two values.
But == is extraneous with booleans. you can simply do if(b) instead of if(b == true). Similarly, you can do if(!b) instead of if(b == false).
You say that odd always evaluates to false, right? Why is that? Trace through your program and figure out where that value comes from. When is it assigned? Why?
Re: problem in my while loop code
It was told to you before: use nextLine() instead of next() if you want to read a String from the input; also read the API documentation for the methods of the Scanner class.
kind regards,
Jos
Re: problem in my while loop code
Thank you both, your explanations are very clear and helpful. It worked! Thanks so much!