File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes conditional String query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "conditional String query " Watch "conditional String query " New topic

conditional String query

Rupert matthews

Joined: Feb 26, 2008
Posts: 9
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

Joined: Oct 02, 2003
Posts: 11955

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 ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rupert matthews

Joined: Feb 26, 2008
Posts: 9
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.

Henry Wong

Joined: Sep 28, 2004
Posts: 20525

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: conditional String query
jQuery in Action, 3rd edition