简体   繁体   English

在数组中进行完整性检查?

[英]Doing an integrity check in an array?

Doing a rather large assignment for my java class. 为我的java类做一个相当大的任务。 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. 我编写了整个程序,但是我遇到了最后一个问题,需要检查用户输入到数组中以确保它是用户输入的整数,而不是垃圾(a, Z,2.0,@%&@#%* @%^等)如果发生错误,它必须循​​环返回,使输入带有错误消息,直到它们符合要求。

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. 我听说使用try / catch作为解决方案,我也想过可能是一个while循环,但仍然不确定如何去做。 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. 由于您使用了Scanner.nextInt() ,因此无法将非整数输入添加到数组中。 Additionally, it is not possible that a non-integer value got into your int[] . 此外,非整数值不可能进入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: 使用Scanner安全读取用户的整数输入的正确方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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