aspose file tools
The moose likes Beginning Java and the fly likes Capturing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Capturing "Enter Key" or "Blank" in Switch Statement" Watch "Capturing "Enter Key" or "Blank" in Switch Statement" New topic
Author

Capturing "Enter Key" or "Blank" in Switch Statement

Ken Huskerson
Greenhorn

Joined: May 16, 2004
Posts: 4
Hello,

I am looking for a way to capture the enter key for user input used in a switch statement.

The problem is when a user doesn't entere a valid value and just presses the enter key.

I though it might be capture as:

if(action = null || action = " ")

but neither are working.

Is there some kind of escape character I should be checking for?

Thanks in advance!

kh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24059
    
  13

Hi Ken ,

Welcome to JavaRanch!

First, if you're reading lines from standard input using a BufferedReader or something similar, then if the user just presses Enter, you'll get an empty string, "" (no space between the quotes.)

Second, to compare String objects, you need to use the equals() method, and not the "==" operator. The former compares contents of Strings; the latter tells you if the two strings are the same physical object in memory.

So you're looking for something like

if (action == null || action.equals(""))


[Jess in Action][AskingGoodQuestions]
Ken Huskerson
Greenhorn

Joined: May 16, 2004
Posts: 4
I'm embarrassed. I should have know that. Thank you for the quick response.



kh
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
No need to feel embarrassed. It's for questions like these that the Beginners forum exists.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Capturing "Enter Key" or "Blank" in Switch Statement
 
Similar Threads
enter button doesn't work on select boxes
getting action from field without pressing enter
Capturing "Enter Key" Value in Javascript
Submitting with "Enter" from textbox where there are several submit buttons on form
Enter Key Code