0

I've just begun to learn how to program with Java and I had a question with regard to the scanner input. I'm building a little program that simply asks the user for input to create a numerical array. I was wondering if there was a way to check for the numerical input encompassing the for loop, instead of putting a while check on each of my cases in the for loop.

As well, any other comments or suggestions on my code to help me improve and understand what I am doing would be greatly appreciated!

Thank you!

Edit: I'm calling this class from a 'Main' class where I run the program.

import java.util.Scanner; //Import the use of the Java Scanner

public class ArrayBuild { // Open Application

    private static Scanner input;

    public Double[] anArray;

    public static int arrayCount = 0;

    public ArrayBuild() { // Constructor for ArrayBuild object

        input = new Scanner(System.in);

        arrayCount++;

        System.out.println("This will be Array: " + arrayCount);

        // Array Size Declaration
        System.out.println("Enter Array Size: ");
        while (!input.hasNextInt()) {
            System.out.println("Please enter an integer for Array size!");
            input.next();
        }
        int n = input.nextInt();
        anArray = new Double[n]; // Create 'anArray' of size n
        //

        for (int i = 0; i < n; i++) { // Begin For Loop

            if (i == 0) {
                System.out.println("Enter First Number: ");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }

            else if (i > 0 && i < (n - 1)) {
                System.out.println("Enter Next Number: \n");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }

            else if (i == (n - 1)) {
                System.out.println("Enter Final Number: ");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }
        } // End For Loop
    }
} // Close Class
1
  • Read the javadoc for all the nextX() methods. They explain what they actually read. Commented Sep 28, 2013 at 16:29

1 Answer 1

2

One thing you can do to simplify and write clean code is to always separate the repeating code. In your case, inside the for loop, you are only changing the print statement inside the if condition. Take the other code outside like this--

for (int i = 0; i < n; i++) { // Begin For Loop

        if (i == 0) 
            System.out.println("Enter First Number: ");

        else if (i > 0 && i < (n - 1))
            System.out.println("Enter Next Number: \n");

        else if (i == (n - 1)) 
            System.out.println("Enter Final Number: ");

        while (!input.hasNextDouble()) {
                System.out.println("Please enter a number for array data!");
                input.next();
        }
        Double D = input.nextDouble();
        anArray[i] = D;

    } // End For Loop
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh!! Such an obvious answer! Thank you so much. It seems I have a lot to learn, and how I look at code is among the most important!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.