1

Hello java pros and experts, the first problem I have is implementing a code into my program where it tells a user "No numbers were entered" when they do not enter any numbers and the array is empty.

the second problem I have is implementing a code if the user enters too many numbers, put out an Error message that the size of the array has been exceeded; then print out the numbers entered on the next line.

This is my code so far:

 import java.util.Scanner;

     public class FunWithArrays
    {
        public static void main(String[] args)
        {
            final int ARRAY_SIZE = 10; // 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++)
            {
                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] + " ");
    }
}

I was wondering if you pros or experts have any advice on how to approach this problem without changing any of the codes in my program? my professor instructed me to do a do-while loop for the problem, but do not know how to create the code for it... any suggestions?

C.Lai
  • 11
  • 1
  • 5
  • This isn’t your question, but you might need to read this: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – achAmháin Nov 27 '18 at 21:57
  • What do you define as 'no numbers' or 'too many' numbers? Because currently you are forcing the user to only enter exactly 10 numbers, and anything contrary to just that will not work (letters, symbols = crash, and no choice to input > or < than 10 numbers) – M.G Nov 27 '18 at 21:59
  • @M.G I define no numbers by when they do not enter a number when the user is suppose to enter a number in an array, and too many numbers as in if they tried to enter more then 10 then a message will pop up saying they exceeded the amount and print the 10 numbers they entered – C.Lai Nov 27 '18 at 22:04
  • You are not giving the user an opportunity to enter no numbers or more than 10. The loop is forcing them to put in exactly 10 numbers and no more, and if they put anything but a number your program crashes (can catch this error and print your desired message) – M.G Nov 27 '18 at 22:06
  • @M.G do you have any suggestions on how to go upon this problem. I've been stuck on this for a while :/ – C.Lai Nov 27 '18 at 22:07
  • You can take all the users inputs at once then check if nothing was entered, or if they entered more than 10 values. Take a read through [this](https://stackoverflow.com/questions/23506429/java-reading-multiple-ints-from-a-single-line) – M.G Nov 27 '18 at 22:17
  • Please have a read through this: https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – SiKing Dec 06 '18 at 21:10

0 Answers0