为什么我的尝试和捕获陷入循环?

时间:2014-02-08 20:17:12

标签: java while-loop try-catch

我希望用户将整数输入到数组中。我有这个循环,我写了一个尝试并捕获它,以防用户插入非整数。有一个布尔变量,如果为真则保持循环。这样,系统将再次提示用户并提示用户。

除此之外,当我运行它时,它会陷入一个循环,它会重复“请输入#”和“需要整数”而不让用户输入新的数字。如果发现异常,我会重置该号码。我不明白。

import java.util.*;
public class herp
{
    //The main accesses the methods and uses them.
    public static void main (String[] args)
    { 
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello and welcome!\n");
        System.out.print("This program stores values into an array"+" and prints them.\n");
        System.out.println("Please enter how many numbers would like to store:\n");
        int arraysize = scan.nextInt();

        int[]   mainarray = new int[arraysize+1];
        int     checkint  = 0;
        boolean notint    = true;
        int     prompt    = 1;

        while (prompt < mainarray.length)
        {
            // Not into will turn true and keep the loop going if the user puts in a
            // non integer. But why is it not asking the user to put it a new checkint?
            while(notint)
            {
                try
                {
                    notint = false;
                    System.out.println("Please enter #"+ prompt); 
                    checkint = scan.nextInt();
                } 
                catch(Exception ex)
                {
                    System.out.println("An integer is required." + 
                                       "\n Input an integer please"); 
                    notint   = true;
                    checkint = 1;
                    //See, here it returns true and checkint is reset.
                }      
            }
            mainarray[prompt] = checkint;
            System.out.println("Number has been added\n");
            prompt++;
            notint = true;
        }
    } 
}

2 个答案:

答案 0 :(得分:4)

扫描仪抛出InputMismatchException后,无法继续使用。如果您的输入不可靠,而不是使用scanner.nextInt()使用scanner.next()来获取String,那么将字符串转换为int。

替换:

checkint = scan.nextInt();

使用:

String s = scan.next();
checkint = Integer.parseInt(s);

答案 1 :(得分:0)

我已在下面更正过了。我不依赖异常,但检查下一个Scanner输入是否为int(使用hasNextInt())。如果不是int,只需使用Scanner令牌并等待下一个用户输入。 看起来它正在工作,除了作为第一个数组元素插入0之外,因为你从1开始索引提示。

public class Herp {

    //The main accesses the methods and uses them.
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello and welcome!\n");
        System.out.print("This program stores values into an array" + " and prints them.\n");
        System.out.println("Please enter how many numbers would like to store:\n");
        int arraysize = scan.nextInt();

        int[] mainarray = new int[arraysize + 1];
        int checkint = 0;
        boolean notint = true;
        int prompt = 1;

        while (prompt < mainarray.length) {
            while (notint) {
                notint = false;
                System.out.println("Please enter #" + prompt);
                // check if int waits for us in Scanner
                if (scan.hasNextInt()) {
                    checkint = scan.nextInt();
                } else {
                    // if not, consume Scanner token
                    scan.next(); 
                    System.out.println("An integer is required."
                        + "\n Input an integer please");
                    notint = true;
                }
            }
            mainarray[prompt] = checkint;
            System.out.println("Number has been added\n");
            prompt++;
            notint = true;
        }

        for (int i : mainarray) {
            System.out.print(i + " ");
        }
    }
}