Results 1 to 5 of 5
Thread: help with loop. yes no
- 02-17-2011, 07:05 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
help with loop. yes no
[RESOLVED!] :) thank you!!
Hey! I'm new to the forum but i recently started learning Java. If anyone could point me in the right direction here i would appreciate it!
Java Code:// asks user to enter yes, if user does not input yes program loops // wile loop style import java.util.Scanner; public class Test1 { public static void main(String[] args) { String repeat; Scanner keyboard = new Scanner(System.in); System.out.println("Enter Yes to continue"); repeat = keyboard.nextLine(); while (repeat != 'Yes') { System.out.println("Enter Yes continue"); repeat = keyboard.nextLine(); } } }
what am i doing wrong? :confused:Last edited by glina126; 02-17-2011 at 07:50 AM. Reason: resolved
- 02-17-2011, 07:10 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Don't use != to compare Strings (or any other objects) use equals() or !equals().
equals() is a method declared for class Object but other classes define their own version to mean what makes sense for them. In the case of String there is an equals() that means "is made up of the same characters in the same order".
- 02-17-2011, 07:12 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Oh and you can't use single quotes for String literals.
So,
Java Code:while (!repeat.equals("Yes")) { // etc
- 02-17-2011, 07:49 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
thanks so much!!!!!!!!!! i like the quick help and great responses! :D thanks!!!
- 02-17-2011, 10:24 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
You're welcome.
(and welcome to the forum!) Just a tip: if you compile (and you should compile often...) and get a message you can't understand, post the entire compiler message. This would have alerted me to the single quotes. Likewise, *describe* the problem ie say what the undesired program behaviour is. ("doesn't stop when I enter Yes").
Programmers are very literal minded and like to be given the facts. (even the obvious ones). But there is another reason for being precise and specific: I find with the mistakes that I make that I am often closer to figuring out an answer when I have asked *myself* a precise and specific question. Being able to frame such questions (and doing so is neither easy nor natural) is a good problem solving technique.
(this rant/musing is not directed at you specifically - it just occured to me when I reread your original post and I thought would express it for the benefit - or otherwise - of all ;)
Again, welcome.Last edited by pbrockway2; 02-17-2011 at 10:26 PM.
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks