wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to check for null value entered by user in input in eclipse 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 "How to check for null value entered by user in input in eclipse" Watch "How to check for null value entered by user in input in eclipse" New topic
Author

How to check for null value entered by user in input in eclipse

kar ja
Greenhorn

Joined: Sep 20, 2012
Posts: 1
Hi All,

I have written the following code which takes a character from the user and searches for the character's occurrence in a file.The code is written in Eclipse.

It works fine if the user inputs something but if the user does nt input anythin and simply presses enter it throws an exception.Can anyone please help:Output given in the end .


Output:

Enter the character you want to find!

input:
Exception encountered!String index out of range: 0
changu mani
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
Hi Kar ja,
Welcome!
If you are just looking to get rid of Exception, please add isEmpty() in your loop.


Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

You've fallen into one of the evil traps of Strings. Never use == or != to compare two strings to see if they are the same (like you are doing on line 25 with ""). Always use .equals() instead - "Hello" might not refer to the exact same object as "Hello". Try the following, then search around this forum or on the internet; the problem with string equality comes up quite a bit.



Also not sure why you are setting lookfor to "\0"; Java doesn't require strings be null terminated.

And Welcome to the ranch!


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kar ja wrote:
The code is written in Eclipse.


For future reference, that part is irrelevant here, and is irrelevant for almost any question you might have about Java. (The only cases where it matters is where an IDE has its own compiler and that compiler behaves differently from the spec or from the JDK's compiler, which does happen but is rare.) It's basically like asking for help with a math problem and then mentioning that you're using a pencil to write down your work.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
Tina Smith wrote: . . . "\0"; Java doesn't require strings be null terminated.

And Welcome to the ranch!
Good catch. That is quite right. In fact it should be very difficult to get a \0 into a Java String.

kar ja, why did you expect there to be a \0 in the String in the first place? They might exist in C/C++ but Java is a completely different language.
As well as Eclipse in the thread title being misleading, so is the use of the word null. You are not entering null Strings, but 0‑length Strings, which are very different.
Maybe you should write yourself a utility class. You can find examples here, here and here. There are others if you search. You will want a method different from anything you have seen there, namely public static String getStringFromKeyboard(String message, String errorMessage, int minimumLength). You can use that method to get keyboard input of a certain length, and you can use it again for your other applications.
 
 
subject: How to check for null value entered by user in input in eclipse
 
Similar Threads
Searching Strings in a textfile
no such element? eek!
Problem in file transfer
declaring array of vectors
Files from a directory is not being recognized