aspose file tools*
The moose likes Beginning Java and the fly likes using String/char in boolean statment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using String/char in boolean statment" Watch "using String/char in boolean statment" New topic
Author

using String/char in boolean statment

gaine chung
Greenhorn

Joined: Sep 16, 2001
Posts: 6
simple question from a simple mind: How do you use a String/char in a boolean statment? ie: if (userInput == 'N') {do something};
[This message has been edited by gaine chung (edited October 24, 2001).]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

use == for char comparison:
<pre>
char userInput = ...
if (userInput == 'N') { // do something }
</pre>
but use equals() when comparing String:
<pre>
String userInput = ...
if ("N".equals(userInput)) { // do something }
</pre>
The above form is safer than writing
<pre>userInput.equals("N")</pre>
because the latter will produce a NullPointerException if userInput is null.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform


Junilu - [How to Ask Questions] [How to Answer Questions]
gaine chung
Greenhorn

Joined: Sep 16, 2001
Posts: 6
That was exactly what I was looking for. Thanks junilu!
- Gaine
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using String/char in boolean statment