Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By JosAH

Thread: problem in my while loop code

  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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?
    JMAsterson likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,775
    Blog Entries
    7
    Rep Power
    21

    Default 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
    JMAsterson likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: problem in my while loop code

    Thank you both, your explanations are very clear and helpful. It worked! Thanks so much!

Similar Threads

  1. Replies: 18
    Last Post: 04-03-2012, 10:49 AM
  2. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  3. How to loop this CODE using do while
    By antidup in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 05:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •