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 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using String/char in boolean statment" Watch "using String/char in boolean statment" New topic

using String/char in boolean statment

gaine chung

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

Joined: Feb 26, 2001
Posts: 4419

use == for char comparison:
char userInput = ...
if (userInput == 'N') { // do something }
but use equals() when comparing String:
String userInput = ...
if ("N".equals(userInput)) { // do something }
The above form is safer than writing
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

Joined: Sep 16, 2001
Posts: 6
That was exactly what I was looking for. Thanks junilu!
- Gaine
I agree. Here's the link:
subject: using String/char in boolean statment
Similar Threads
Continue Statement
converting char to String
about char declair
Wait for input from JTextField before continuing with code
Hashing Problem