Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kar ja
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kar ja,
Welcome!
If you are just looking to get rid of Exception, please add isEmpty() in your loop.


 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic