简体   繁体   中英

Doing an integrity check in an array?

Doing a rather large assignment for my java class. I've written the entire program, but I've run into one last problem, there needs to be a check on the user input into the array to make sure it is an integer that the user is inputting, and not junk(a, Z, 2.0, @%&@#%*@%^, etc.) and if that error happens, it has to loop back, reallowing the input with an error message until they comply.

I've heard of using try/catch to try as a solution, and I've also thought of maybe a while loop, but still not sure on how to go about it. Any tips?

import java.util.Scanner;
import java.util.Arrays;

public class Assignment6 {
    public static int getMaxValue(int[] array) {
        int maxValue = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > maxValue) {
                maxValue = array[i];
            }
        }
        return maxValue;
    }

    public static int getMinValue(int[] array) {
        int minValue = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < minValue) {
                minValue = array[i];
            }
        }
        return minValue;

    }

    public static double average(int[] array) {
        double sum = 0, average = 0;
        for (int i = 0; i < array.length; i++) {
            sum = sum + array[i];
            average = sum / array.length;
        }
        return average;
    }

    public static double Median(int[] array) {
        int Median = array.length / 2;
        if (array.length % 2 == 1) {
            return array[Median];
        } else {
            return (array[Median - 1] + array[Median]) / 2.0;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter size of array: ");
        int[] array = new int[input.nextInt()];

        for (int i = 0; i < array.length; i++) {
            System.out.print("Enter value #" + (i + 1) + ": ");
            array[i] = input.nextInt();
        }

        System.out.println("\nYour data is:");

        for (int i : array)
            System.out.print(i + "\n");
        System.out.println();

        System.out.println("Average is : " + average(array));

        System.out.println("Smallest value is: " + getMinValue(array));

        System.out.println("Largest value is: " + getMaxValue(array));

        System.out.println("\nYour sorted data is: ");

        Arrays.sort(array);

        for (int i = 0; i < array.length; i++)
            System.out.println(array[i]);

        double Median = Assignment6.Median(array);
        System.out.println("\nThe median is: " + Median);

        int Range = Assignment6.getMaxValue(array) - Assignment6.getMinValue(array);
        System.out.println("\nThe range is: " + Range);

        double midRange = (Assignment6.getMaxValue(array) + Assignment6.getMinValue(array)) / 2.0;
        System.out.println("\nThe Midrange is: " + midRange);
    }
}

Since you used Scanner.nextInt() , it is not possible that non-integer input was added to the array. Additionally, it is not possible that a non-integer value got into your int[] . The program would likely not compile, and even if it did, would throw a run-time exception.


However, if the user inputs a non-integer, your program will crash. It doesn't really have anything to do with an array, but I'm guessing this is what you meant by your question. The correct way to safely read only integer input from the user with a Scanner would be:

for (int i = 0; i < array.length; i++) {
    System.out.print("Enter value #" + (i + 1) + ": ");

    while (!input.hasNextInt()) { // <-- 'peeks' at next token
        System.out.println("Please enter an integer!");
        input.next(); // <-- skips over invalid token
    }

    array[i] = input.nextInt();
}

您也可以使用Integer.parseInt(yourString),如果String不是有效的,它将抛出NumberFormatException

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