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

Thread: How to limit the input numbers?

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to limit the input numbers?

    Suppose I want to limit the input numbers to 20 in the following program, what should I do?


    private static void Question2(){
    Scanner scr = new Scanner(System.in);
    int count = 0;

    System.out.println("How many numbers are you going to enter (not greater than 20)");
    int n = scr.nextInt();
    int[] num = new int[n]; // num is an array of n integers

    System.out.printf("Enter %d numbers\n", n);
    for (int i = 0; i < num.length; i++) {
    num[i] = scr.nextInt();
    }

    System.out.println("Pick a number");
    int x = scr.nextInt();

    for (int i = 0; i < num.length; i++) {
    if(num[i] == x) {
    count++;
    }
    }
    System.out.printf("You entered %d occurence of %ds\n", count , x);

    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to limit the input numbers?

    Check the value of n and if it is greater than 20, inform the user and ask for a new number.

    Regards,
    Jim

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to limit the input numbers?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: February 8th, 2014, 04:59 AM
  2. Input Validation - Take no Symbols or Numbers
    By ProgrammablePeter in forum Java Theory & Questions
    Replies: 4
    Last Post: December 29th, 2013, 04:01 AM
  3. [SOLVED] HELP - Summing Numbers, with and without Input
    By SunnyS in forum Loops & Control Statements
    Replies: 3
    Last Post: October 18th, 2012, 04:01 PM
  4. How do i limit the input time
    By itayj in forum Java Theory & Questions
    Replies: 6
    Last Post: October 24th, 2011, 03:47 PM
  5. HELP! Input, numbers, nonsense
    By Corvus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2010, 03:18 PM