Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Help with user input

    I am trying to write a program that uses a user's integer input to run a program. Everything works fine but when the user inputs a letter or word rather than an integer my program stops and lists a bunch of "exceptions":
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at xxxx.main(xxxx.java:58)

    How would I go about fixing this so that the programs asks for a valid integer input.

    I am using eclipse if that matters.

    Thank you very much

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Catch exception and ask user for right input in while-loop.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Java Code:
    try{
        //read user input, convert to int
    }catch(NumberFormatException e){
        //Tell the user they need to input only integers and try again
    }
    Something like that. You might consider putting this in a loop that terminates when the correct input is supplied.

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  3. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Tags for this Thread

Posting Permissions

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