Results 1 to 6 of 6
  1. #1
    doymand is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Preventing errors when typing a letter into a String

    I would like general guidance on how to avoid an error when someone tries to type a letter into an integer from the scanner class. My program asks for a number, but if someone types a letter in it gives multiple errors and I would like to know how to avoid this.

    I think I would use a try, catch, but I am not exactly sure how to implement that. What error do I put in the catch? Can I just use one statement for all errors?

    I was thinking something like this

    try{
    x = scanner.nextInt}

    catch(Exception x){
    }

    What do I put in the spot where I have exception?
    Last edited by doymand; 10-30-2010 at 03:52 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    You could try something like:

    Java Code:
    String example = "12a";
    try
    {
         int temp = Integer.parseInt(example);
    }
    catch(NumberFormatException e)
    {
         System.out.print(e);
    }

    In short, it tries to convert your string to an integer number. If it can't then it will throw an error meaning your string is not a number.
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    Here is what you need:

    Scanner scan = new Scanner(System.in);
    int number = 0;

    System.out.print("Enter a number: ");

    try
    {
    number = scan.nextInt();
    }
    catch(InputMismatchException e)
    {
    System.out.println("A Non-numeric data was entered");
    System.exit(0);
    }

    System.out.println(number);

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Honopac, at least let them try to figure it out...
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    Next time I will try to give them just a hint, but not the actual code. This is the best way to help them learn how to solve programming problems on their own.

Similar Threads

  1. Preventing local copies of fields
    By Steve11235 in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-05-2009, 05:51 AM
  2. Replies: 18
    Last Post: 07-12-2009, 05:47 PM
  3. preventing access/locking a folder?
    By solris in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 12:20 AM
  4. Replies: 20
    Last Post: 05-14-2008, 09:42 AM
  5. Replies: 2
    Last Post: 08-10-2007, 12:36 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
  •