• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using input value for user validation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to take an input from the user and validate it to make sure it is not a null value. I will also have to convert a string to an integer. I have been trying to use scanner to get this result but with limited success. Any suggestions?

 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in the code you've posted, you check whether your Scanner named "input" is null. This can never possibly happen, as you initiate it three lines above:

I assume you wanted to check whether the String was null, but this isn't the String variable. As a reminder, integers cannot be null as they are primitives, not objects.


Do you know what this line of code does, and what the args are? Because, based on the line above (where you create a Scanner), it seems like you want to use the Scanner to get input, but the args is something very different. Also, if you're using a Scanner, the input can never really be null. It can only be an empty string (if the user presses Enter when it's empty). For args, it can be null, but I wouldn't use those anyways.


This line occurs before you check if it's null or not. So, if the user entered null, you would get a NullPointerException here despite your checks (because they take place afterwards)

The string being null isn't the only bad thing that can happen when casting a String to an int, though. What happens if the user typed "abc" in? This isn't null, but obviously not something we can make into a number either. The answer here is to use a "try" statement, which will attempt to execute commands in its block. If they fail, then the exception will be "caught" by the catch block further down, and then can be handled as desired.

Assuming everything worked as intended right now, what would happen if the user entered a wrong number? It would display the message, then exit. However based on the message it looks like you want to give the user another chance to enter data, in which case you would need to use a loop, so you'll need to change the logic of this program a bit.


This code looks good. Only one change I would suggest: remove the second else if, and just put it as "else", since the second condition is always true if the first is not true.


You don't really need another function for a single line of code - it doesn't shorten the program at all, and might actually take away readability.

-------

I would recommend rewriting the program, keeping the above problems in mind. I've made a method stub to hopefully help you:
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find out how this line of code works:-
System.out.println("The number is " + (i % 2 == 0 ? "even." : "odd."));
It can save you lots of writing.

Did you know you can use the & operator to find whether a number is odd or even? You simply find out whether the rightmost bit is 1 or 0:-
(i & 1) == 0
You must use the () in that expression because == has a higher precedence than &

The input from a Scanner pointing to System.in can never be null. You can get the empty String "" but never null. If there is no input available, it will still not return null. You can find out what happens with this sort of code.The only way to escape that loop is to feed ctrl‑D (*nix) or ctrl‑Z (Windows®); try it and see what happens.
I suspect a Scanner will not return null from other input sources, either.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic