This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 4447
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using String/char in boolean statment
 
Similar Threads
Hashing Problem
Continue Statement
about char declair
converting char to String
Wait for input from JTextField before continuing with code