Results 1 to 6 of 6
  1. #1
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default While loop not looping need help

    This loop will only go through the first time and when the user types Y at the bottom it wont reloop even though the value for ques is Y.


    Java Code:
    	
            String ques = "Y";
            while (ques == "Y") {
      	
    	System.out.println("Enter a word.");
    
    	String str = br.readLine();
    	str = str.toUpperCase();
    
    	if ( ( str.indexOf('R') == -1 ) || ( str.indexOf('N') == -1) ) {
    	  System.out.println("The word does not contain an 'r' and an 'n'.");
    	}else if ((str.indexOf('R')) < (str.indexOf('N'))) {
    	  System.out.println("'R' comes before 'N' in: " + str);
    	}else{
    	  System.out.println("'N' comes before 'R' in: " + str);
    	}
    	  System.out.println("Continue? (Y/N)");
    
    	  ques = br.readLine();
    	  System.out.println(ques);
    
    	
      }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't use == to compare Strings but rather either the equals or equalsIgnoreCase method. Also, please fix your code indentation so we are able to read and understand it.

  3. #3
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    thank you and sorry im not sure how its supposed to be indented

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    All loops and all blocks get indented so it's easy to distinguish that a loop or block exists there. So it's not

    Java Code:
    while {
    more code
    }

    but rather
    Java Code:
    while {
       more code
    }

    But more importantly, have you tried using the equals method and if so, what happens?

  5. #5
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    i used the equals method and it worked thanks for all the help

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Problem with looping
    By jarredh4 in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 04:56 AM
  2. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  3. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  4. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 09:48 AM
  5. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM

Posting Permissions

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