Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to limit users to only enter integer into a String variable.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to limit users to only enter integer into a String variable.

    Hi Java Professionals,
    I need to allow users only enter integer into a String variable, "input" and I am not sure what statement to use. Please help, thanks a lot in advance.

    import java.util.Scanner;
    public class assq2b
    {
    public static void main(String []args)
    {
    Scanner reader = new Scanner(System.in);
    String input,b;
    long checked;
    System.out.print("Please enter the 12 digit:");
    input = reader.nextLine();
    if((input.length() < 12) || (input.length() > 12))
    {
    System.out.println("The entered digits is not equal to 12.");
    System.exit(0);
    }
    else
    {
    System.out.println("Calculating...");
    }

    checked =Long.parseLong(input);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to limit users to only enter integer into a String variable.

    You can't control what a user enters, but you can make the program not accept what the user enters.
    To do that use a loop that asks the user for input, tests what is input and exit the loop if the input is acceptable. If the input is not acceptable, the loop goes back and asks the user for input again.

    One way to test the user's input to be a number is to use the Integer class's parse method inside of a try catch block.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to limit users to only enter integer into a String variable.

    Hi,
    I amended my code but I also need to make the program only accept a 12 digit number and currently it doesn't accept "098765432199".It means the first digit cannot be zero,please help.

    Scanner reader = new Scanner(System.in);
    String input=" ",b;
    long checked;

    do {
    System.out.print("Please enter a 12 digit number:");
    while (!reader.hasNextLong())
    {
    System.out.println("That's not a number!");
    System.out.print("Please renter 12 digit number:");
    reader.next();

    }
    checked =reader.nextLong();
    input = Long.toString(checked);
    }while (input.length() != 12);
    System.out.println("Thanks");

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: How to limit users to only enter integer into a String variable.

    you need to get the users input before you start the validation so you have something to check it against

    you are using a do while loop wrong

    do { <--start loop
    stuff you want to do at least one time <-- ask for input and receive the input
    }while(test here); <---test if the input is good and if its not go back to do and start all over

    really you could just use a while loop after you get the users input and if the while loop tests incorrect to ask for input again..then if the users input is correct it wouldn't even execute the body of the while loop

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to limit users to only enter integer into a String variable.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Its hard to see the logic without the code properly formatted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Limit length of characters output in JSP string
    By me10lee83 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 24th, 2013, 09:45 PM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. Java integer limit
    By jobi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 4th, 2013, 03:27 PM
  4. Synchronized a integer variable in Java
    By hellokitty25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 24th, 2013, 09:49 PM
  5. keyboard scanner to take users input and enter students information method
    By foresboo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 7th, 2013, 04:39 PM

Tags for this Thread