Results 1 to 6 of 6
Thread: While loop not looping need help
- 03-06-2011, 03:34 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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); }
-
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.
- 03-06-2011, 03:37 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
thank you and sorry im not sure how its supposed to be indented
-
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?
- 03-06-2011, 09:24 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
i used the equals method and it worked thanks for all the help
-
Similar Threads
-
Problem with looping
By jarredh4 in forum New To JavaReplies: 2Last Post: 02-16-2011, 04:56 AM -
Looping
By Dean29126 in forum New To JavaReplies: 3Last Post: 09-08-2010, 02:01 PM -
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
looping and filtering
By javafanatic in forum New To JavaReplies: 14Last Post: 02-09-2010, 09:48 AM -
Looping Help Please
By JonnySnip3r in forum New To JavaReplies: 5Last Post: 01-31-2010, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks