• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic