Home > Back-end >  How to validate scanner so only non negative doubles are added to array?
How to validate scanner so only non negative doubles are added to array?

Time:09-09

import java.util.Scanner;

public class ArrayReview {
public static void main(String[] args){
    double[] doublesArray = createArray();
    printArray(doublesArray);
}

//"input method" create array of 10 doubles
public static double[] createArray() {
    Scanner userInput = new Scanner(System.in);
    double[] array = new double[10];
    System.out.println("Input 10 non-negative double numbers");
    for(int i = 0 ; i < array.length ; i  ) {
        //input validation
        while(!userInput.hasNextDouble()) {
            userInput.nextLine(); //throws away bad input
            System.out.println("Please input a valid double input");
        }
        if(userInput.hasNextDouble()) {
            array[i] = userInput.nextDouble();
        }
    }
    userInput.close();
    return array;
}

//print all values in an array of doubles
public static void printArray(double[] array) {
    for(int i = 0 ; i < array.length ; i  ) {
        System.out.print(array[i]   " ");
    }
}
}

I need to validate all the users input so that only non negative numbers are allowed into the array. Currently I was able to check if the user inputs a letter or special character but negative numbers are still able to go into the array.

I've tried using the "<" operand in the while statement to see if the userInput<0 but i'm getting an error if i do that. Would I be better off making a method that is solely dedicated to checking if its a negative or not or is there an easier way?

CodePudding user response:

You could extend the conditional expression of the while loop as follows:

for (int i = 0; i < array.length; i  ) {
    double value;
    while (!userInput.hasNextDouble() || (value = userInput.nextDouble()) < 0) {
        userInput.nextLine(); // throws away bad input
        System.out.println("Please enter a valid, non-negative double value");
    }
    
    array[i] = value;
}

CodePudding user response:

I tried using do-while instead of while. So I can access the input first.

public static double[] createArray() {
        Scanner userInput = new Scanner(System.in);
        double[] array = new double[10];
        double value;
        boolean isValid;
        System.out.println("Input 10 non-negative double numbers");

        for(int i = 0 ; i < array.length ; i  ) {
            //input validation
            do {
                value = userInput.hasNextDouble() ? userInput.nextDouble() : 0;
                if(value > 0) {
                    array[i] = value;
                    isValid = true;
                }else {
                    System.out.println("Please input a valid double input");
                    isValid = false;
                }
            }while(!isValid);

        }
        userInput.close();
        return array;
    }

CodePudding user response:

My approach would be, in the for loop of your createArray method to first get the input, then use while loop to see if input is less than 0 in which case you will prompt the user to input a positive number. This will loop until a positive input is received.

I have tested it on my end and seem to get the results you are looking for. Your createArray method should look something like this:

public static double[] createArray() {
    Scanner userInput = new Scanner(System.in);
    double[] array = new double[10];
    System.out.println("Input 10 non-negative double numbers");

    for(int i = 0 ; i < array.length ; i  ) {
        //input validation
        double input = userInput.nextDouble();
        while (input < 0) {
            System.out.println("Please input a positive number");
            input = userInput.nextDouble();
        }
        array[i] = input;
    }

    userInput.close();
    return array;
}
  • Related