This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Trouble with String comparison  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare the variable outside the loop, and initialize it. Something like :
 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!