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 7 of 7

Thread: How to limit users to enter only 12 digit input and an integer value in Java?

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

    Default How to limit users to enter only 12 digit input and an integer value in Java?

    Hi Java professionals,
    I want to limit the program to only accept a 12 digit input and an integer value but I don't know how to program it,please help.

    import java.util.Scanner;
    public class testing4
    {
    public static void main(String []args)
    {
    Scanner reader = new Scanner(System.in);
    String input;

    do {
    System.out.print("Please enter a 12 digit number:");
    input = reader.nextLine();
    }while( input.length()!=12);
    System.out.println("Thanks.");
    }
    }


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: How to limit users to enter only 12 digit input and an integer value in Java?

    hi,

    you can use the if condition after the getting input from the user.
    if(input.length()==12)
    {
    //allow
    }

    Still you have doubts or queries post here pls.

    Thanks,

  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 enter only 12 digit input and an integer value in Java?

    Hi,
    but I need to loop it if the input is incorrect,do you have any other advice?

    --- Update ---

    I mean the program needs to loop and it only accepts a numeric input.

  4. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: How to limit users to enter only 12 digit input and an integer value in Java?

    yes you can use your same condition in do..while
    do { 
    System.out.print("Please enter a 12 digit number:"); 
    input = reader.nextLine(); 
    if(input.length()==12){
    	//allow
     
    }
     
    }while( input.length()!=12);

    Thanks,

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

    Default Re: How to limit users to enter only 12 digit input and an integer value in Java?

    Thanks but my program must not accept any letter or symbol and it must only accepts numbers,please advise.

  6. #6
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: How to limit users to enter only 12 digit input and an integer value in Java?

    Hi,

    K do one thing, try to parse your input to integer using Integer.parseInt(your_input). Use try and catch statement.

    If you entered string means it throws an error in catch statement you can display "It is not a valid number" otherwise program flow goes well.

    do
    {
    try
    {
    //getInput
    //use Integer.parseInt(getInput) convert to int
    }
    catch(Exception e)
    {
    //Print "It is not a valid number"
    }
    }while(**Yourconditon**);

    If you face any difficulties post here. Happy to help

    Thanks,

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to limit users to enter only 12 digit input and an integer value in Java?

    @Loh Jane: Please post your code in code/highlight tags. You can learn how with other useful info for newcomers at this link.

Similar Threads

  1. How to limit users to only enter integer into a String variable.
    By Loh Jane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2014, 09:50 PM
  2. ISBN-10 enter 9 digit to find the 10th digit
    By cldance5678 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2013, 02:52 PM
  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. 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
  5. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM

Tags for this Thread