Results 1 to 6 of 6
  1. #1
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Is there a way to make a user only input an int?

    I want to only allow the user to input an int and then if they enter a character which isn't an int i want an error message to appear. How can i do this?

    Thanks.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Is there a way to make a user only input an int?

    Hello Harley,

    this is very much possible to do. If you specifically want to check if it's an integer I'd suggest that you look into parsing the input and then have a look at the class NumberFormatException.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Is there a way to make a user only input an int?

    The only way to make a user enter an int is to apply a screwdriver to their keyboard.

    if they enter a character which isn't an int i want an error message to appear
    This is a much more realistic design goal.

    Assuming we are talking about a console based program...

    * Read the input the user enters. (Eg as a character stream, Console, or Scanner. Pick one, or try all three.)
    * Convert the input to an int. Scanner has a method that does this for you. Otherwise you convert the String to an int. Oracle's tutorial suggests one way, but I'd go for using the parseInt() method of the Integer class.
    * Catch any exception that will occur if what they enters does not convert (ie is not an int).

    I hope I haven't confused matters by pointing out so many alternatives. A common approach, these days, seems to be to use a Scanner and its nextInt() method.

  4. #4
    Reddi.Java is offline Member
    Join Date
    Dec 2012
    Location
    India
    Posts
    6
    Rep Power
    0

    Default Re: Is there a way to make a user only input an int?

    for your problem,we can handle through java script also

    like below e.g

    function validateForm()
    {
    var x=document.forms["myForm"]["fname"].value;
    if (x==null || x=="")
    {
    alert("First name must be filled out");
    return false;
    }
    }

    The function below checks if a field has been left empty. If the field is blank, an alert box alerts a message, the function returns false, and the form will not be submitted:

    now you can try fo your case too.

    i think this will help for solve your problem.

    regards,
    Reddi

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

    Default Re: Is there a way to make a user only input an int?

    Quote Originally Posted by Reddi.Java View Post
    for your problem,we can handle through java script also
    I can do it in Lisp, Algol, Fortran, BCPL and more than a dozen other languages; they all don't apply here just as JavaScript doesn't apply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Is there a way to make a user only input an int?

    Right, thanks for all your help guys, I'll have a look into some of the methods.

    If i'm ever good enough and you guys need help, I'll definite help you! :)

    Thank you!

Similar Threads

  1. How to make user input be of the type double?
    By abv in forum New To Java
    Replies: 9
    Last Post: 08-06-2012, 10:24 AM
  2. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 08:08 PM
  3. Replies: 2
    Last Post: 09-27-2011, 07:46 PM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 12:02 AM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 05:47 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
  •