aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with String comparison" Watch "Trouble with String comparison" New topic
Author

Trouble with String comparison

Wouter Hermans
Greenhorn

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:

!String1.equals(String2)

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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


[My Blog]
All roads lead to JavaRanch
Wouter Hermans
Greenhorn

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: http://aspose.com/file-tools
 
subject: Trouble with String comparison