This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Trouble with String comparison
jQuery in Action, 3rd edition