File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Trouble with String comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with String comparison" Watch "Trouble with String comparison" New topic

Trouble with String comparison

Wouter Hermans

Joined: Oct 16, 2010
Posts: 13
Hi all,

I made a little Magic 8Ball program which runs okay - it asks for input, then displays a random result from an array of responses (like the real thing, basically). I'm now trying to expand it so it keeps taking input and prints responses until the user types "q". To do this I'm using a While which checks if the input String is not equal to q, so:


But this throws an error saying blabla can't be resolved (blabla is the input from the scanner):

Spot the newbie error and help a poor lost soul.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You need to declare the variable outside the loop, and initialize it. Something like :

[My Blog]
All roads lead to JavaRanch
Wouter Hermans

Joined: Oct 16, 2010
Posts: 13
Thanks, that got the job done!

Here's the code so far; it works, but it probably needs some cleaning.

The prompt asks you to type a question. q prints "have a nice day" in Dutch (when I first tried the do... while, typing q printed a random prediction before quitting, so I added an if...else to replace it with something more proper).

EDIT: sorry, broke the formatting there.
I agree. Here's the link:
subject: Trouble with String comparison
It's not a secret anymore!