Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conditional String query

 
Rupert matthews
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. I just would like to ask why the following code doesn't run the way I expect it:



it compiles but whatever input i do, it simply doesn't display the strings on the condition statements. Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that REALLY your code? why do you use "equals" for the first condition, and "==" for the second?

also, what is 'N'? is that a variable? if so, what is the value of 'N'? Even if it is a reference to a string, odds are you don't want "==" there...

also, and I'm not sure of this, but if you are getting your input for 'again' from the command line, there may be a new-line character on the end (since you do, after all, hit 'enter' on the command line, right?). So you may need to strip is off. I'd try printing 'again' by itself somewhere to make sure it's what you think it is. if you do this:

System.out.println(":" + again + ":");

you can see if the second ':' is on the same line or not...
[ September 25, 2008: Message edited by: fred rosenberger ]
 
Rupert matthews
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sory for that unclean code there
actually this is the real code on my program:



the program intends to accept an input if the user wants to try again. If the user enters Y, the program should say "hi", otherwise.."Bye". However non of this conditions' printlines are displayed. any advice will be very much appreciated.

thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Fred mentioned, you have to use equals. The "==" operator doesn't compare values -- it compares references. And since you are trying to compare different objects, it will always be false.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic