简体   繁体   中英

Error message saying that the array has been exceeded

I was wondering if you guys can give me pointers on how to fix my code. I am trying to put an out an error message that the size of the array has been exceeded when you entered too many numbers. I know I wrote two posts about this, and many people told me to be specific and do it by myself and I decided to do this program by myself instead of asking for help. So I wrote the code, and it came out nice, but how would I do it when it says, "Enter the number 11:" then I enter a number, and it says it has been exceeded and prints out the 10 arrays on the next line.

Input:

import java.util.Scanner;

 public class FunWithArrays
{
    public static void main(String[] args)
    {
        final int ARRAY_SIZE = 11; // Size of the array

        // Create an array.
        int[] numbers = new int[ARRAY_SIZE];

        // Pass the array to the getValues method.
        getValues(numbers);

        System.out.println("Here are the " + "numbers that you entered:");

        // Pass the array to the showArray method.
        showArray(numbers);
    }

    public static void getValues(int[] array)
    {
        // Create a Scanner objects for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a series of " + array.length + " numbers.");

        // Read the values into the array
        for (int index = 0; index < array.length; index++)
        {
        // To tell users if they exceeded over the amount
            if (index > 9)
            {
                System.out.print("You exceeded the amount " + " ");
            }
            else
            {
            System.out.print("Enter the number " + (index + 1) + ": ");
            array[index] = keyboard.nextInt();
            }
        }
    }

    public static void showArray(int[] array)
    {
        // Display the array elements.
        for (int index = 0; index < array.length; index++)
            System.out.print(array[index] + " ");

    }
}

output:

Enter a series of 11 numbers.
Enter the number 1: 3321
Enter the number 2: 3214
Enter the number 3: 213
Enter the number 4: 21
Enter the number 5: 321
Enter the number 6: 321
Enter the number 7: 3
Enter the number 8: 213
Enter the number 9: 232
Enter the number 10: 321
You exceeded the amount  Here are the numbers that you entered:
3321 3214 213 21 321 321 3 213 232 321 0

OK, here is the code to your need, bare in mind that 11th element is not put into an array at all.

  public static void main(String[] args) {
        final int ARRAY_SIZE = 11; // Size of the array

        // Create an array.
        int[] numbers = new int[ARRAY_SIZE];

        // Pass the array to the getValues method.
        getValues(numbers);

        System.out.println("Here are the " + "numbers that you entered:");

        // Pass the array to the showArray method.
        showArray(numbers);
    }

    public static void getValues(int[] array) {
        // Create a Scanner objects for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a series of " + array.length + " numbers.");

        // Read the values into the array
        for (int index = 0; index < array.length; index++) {
            // To tell users if they exceeded over the amount
            if (index >= 10) {
                System.out.print("Enter the number " + (index + 1) + ": ");
                array[index] = keyboard.nextInt();
                System.out.println("\nYou exceeded the amount " + " ");

            } else {
                System.out.print("Enter the number " + (index + 1) + ": ");
                array[index] = keyboard.nextInt();
            }
        }
    }

    public static void showArray(int[] array) {
        // Display the array elements.
        for (int index = 0; index < array.length-1; index++) {
            System.out.print(array[index] + " ");
        }

    }
}

And I have no idea, why do you want it like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM