Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Keith Jackson

Thread: Identifying Invalid input

  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Identifying Invalid input

    I need a bit of code such as else if () that will allow my scanner to be identified as invalid, invalid inputs would be anything that is not and integer ranging from 2 to 10.

  2. #2
    Amok is offline Member
    Join Date
    Sep 2012
    Location
    Australia
    Posts
    12
    Rep Power
    0

    Default Re: Identifying Invalid input

    Steps;

    1. First create a scanner object e.g Scanner sc = new Scanner(System.in)
    2. Read the value from keyboard and store it into int varialbe e.g int value = sc.nextInt();
    3. Now check if value is within the range or not?

    if(value < 2 || value > 10)
    {
    // then this in not in range(Invalid)
    }
    else
    {
    //within the range(valid)
    }

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Identifying Invalid input

    Quote Originally Posted by Amok View Post
    Steps;

    1. First create a scanner object e.g Scanner sc = new Scanner(System.in)
    2. Read the value from keyboard and store it into int varialbe e.g int value = sc.nextInt();
    3. Now check if value is within the range or not?

    if(value < 2 || value > 10)
    {
    // then this in not in range(Invalid)
    }
    else
    {
    //within the range(valid)
    }
    Fine; now suppose I type "foobar"; is that in range? Guess what would happen. Making your code monkey (me) proof takes a bit more ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Amok is offline Member
    Join Date
    Sep 2012
    Location
    Australia
    Posts
    12
    Rep Power
    0

    Default Re: Identifying Invalid input

    @JosAH, we can always put int value = sc.nextInt(); within try/catch block. If its a String then we can catch it within catch block and print a message saying its not in a range. I m sorry if i am wrong. There are many way we can do it and its a programmer choice how to approach to it, thats why i didn't mentioned it.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Identifying Invalid input

    Quote Originally Posted by Amok View Post
    @JosAH, we can always put int value = sc.nextInt(); within try/catch block. If its a String then we can catch it within catch block and print a message saying its not in a range. I m sorry if i am wrong. There are many way we can do it and its a programmer choice how to approach to it, thats why i didn't mentioned it.
    Yep, but keep in mind that if the user didn't type any digits, the nextInt() method won't read anything, so a next call to the nextInt() method chews on those same none-digits again; you have to get rid of them (in the catch-clause). Making input handling really monkey proof takes a bit more than try-catching the conflicting input ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Amok is offline Member
    Join Date
    Sep 2012
    Location
    Australia
    Posts
    12
    Rep Power
    0

    Default Re: Identifying Invalid input

    Thanks alot

  7. #7
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Identifying Invalid input

    So what should I do? lol

  8. #8
    Keith Jackson is offline Member
    Join Date
    Sep 2012
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    Default Re: Identifying Invalid input

    Quote Originally Posted by JosAH View Post
    Yep, but keep in mind that if the user didn't type any digits, the nextInt() method won't read anything, so a next call to the nextInt() method chews on those same none-digits again; you have to get rid of them (in the catch-clause). Making input handling really monkey proof takes a bit more than try-catching the conflicting input ...

    kind regards,

    Jos
    Just remember that once a programmer makes his software "idiot proof", someone comes along with a better idiot.
    pakupakuman likes this.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Identifying Invalid input

    Quote Originally Posted by Keith Jackson View Post
    Just remember that once a programmer makes his software "idiot proof", someone comes along with a better idiot.
    Yes, isn't life cruel?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. identifying a socket exception
    By yemista in forum Networking
    Replies: 2
    Last Post: 12-01-2011, 07:07 PM
  2. Identifying Objects/Variables
    By hiralarious in forum New To Java
    Replies: 2
    Last Post: 09-20-2011, 03:44 AM
  3. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 10:03 PM
  4. Most of my trouble is identifying a pattern
    By ElkNinja in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 06:17 AM
  5. help with identifying components of a gui
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 02:28 AM

Posting Permissions

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