• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Trouble with String comparison

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic