Results 1 to 6 of 6
  1. #1
    SnarfSnarf is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Check for null int

    .....................
    Last edited by SnarfSnarf; 01-30-2009 at 01:16 AM. Reason: n/a

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by SnarfSnarf View Post
    SnarfSnarf: Check for null int
    I have the following code:
    Java Code:
    System.out.print("Please enter your age: ");
    age = keyboard.nextInt();
    // Verify that a value was entered
    ---> if (age == null)
    {
    System.out.println("Please enter a value for age");
    System.exit(0);
    }
    Age is an int. I need the program to exit if the user hits enter without entering a value.
    don't think you can do that.
    i suggest using keyboard.nextLine().
    then if age==null or "" then exit.
    otherwise, parse string into an int.

    OR
    you can use: keyboard.useDelimiter(""); before calling nextInt().
    then put the whole thing in a try/catch block.
    Last edited by angryboy; 02-25-2009 at 07:45 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by SnarfSnarf
    I tried changing age to a string and using keyboard.nextLine() like you recommended but it didn't work. The keyboard.useDelimiter("") sounds too advanced for now, this is our first assignment. I'm trying to figure out a simpler way.
    you can test for zero instead of null.
    so if the user enters 0, the program exits.
    nextInt will always wait for something to be entered.
    Last edited by angryboy; 02-01-2009 at 04:19 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    hmm what happened here? all the post are missing. it could've helped others...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    What caught my attention is "check for null int".

    int is a primitive type, and it cannot be null.

  6. #6
    SnarfSnarf is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    I had to take it down because my professor sent out an email saying that we cannot go to outside sources for help; and this post can be found with google. I did figure it out BTW, I'd send you the answer but I don't have enough posts for a PM.

Similar Threads

  1. Check for printer job
    By pjmorce in forum Advanced Java
    Replies: 0
    Last Post: 11-07-2008, 09:26 AM
  2. How to check multibox value is null
    By tsaswathy in forum Web Frameworks
    Replies: 1
    Last Post: 09-30-2008, 07:26 PM
  3. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 03:37 PM
  4. Can someone check my code
    By joz_12345 in forum Java 2D
    Replies: 7
    Last Post: 02-18-2008, 03:58 AM
  5. spell check
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 12-20-2007, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •