繁体   English   中英

Java,使用 Scanner 尝试捕获

[英]Java, try-catch with Scanner

我正在创建一个小算法,这是其中的一部分。

如果用户输入非整数值,我想输出一条消息,让用户再次输入一个数字:

boolean wenttocatch;

do 
{
    try 
    {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class 
    } 
    catch (Exception e) 
    {
        wenttocatch=true;
        System.out.println("xx");
    }
} while (wenttocatch==true);

我得到了一个永无止境的循环,我不知道为什么。

如何识别用户是否输入了一些非整数?
如果用户输入了一个非整数,如何让用户重新输入?

更新
当我打印异常时,我得到“InputMismatchException”,我该怎么办?

在读取项目之前, Scanner不会前进。 这在Scanner JavaDoc中提到。 因此,您可以使用.next()方法读取值,或者在读取int值之前检查是否有hasInt()

boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);

do {
    try {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class
    } catch (InputMismatchException e) {
        sc.next();
        wenttocatch = true;
        System.out.println("xx");
    }
} while (wenttocatch == true);

你不必尝试捕捉。 此代码将为您解决问题:

public static void main(String[] args) {
    boolean wenttocatch = false;
    Scanner scan = new Scanner(System.in);
    int number_of_rigons = 0;
    do{
        System.out.print("Enter a number : ");
        if(scan.hasNextInt()){
            number_of_rigons = scan.nextInt();
            wenttocatch = true;
        }else{
            scan.nextLine();
            System.out.println("Enter a valid Integer value");
        }
    }while(!wenttocatch);
}

任何时候遇到异常, wenttocatch都会设置为true ,程序就会陷入无限循环。 只要你没有得到异常,你就不会得到无限循环。

如果 sc.nextInt() 导致错误的逻辑是这样的

1) gotocatch 设置为 false

2) sc.nextInt() 抛出错误

3) gotocatch 设置为 true

4) 重复[因为 gotocatch 是真的]

在 catch 语句中解决这个 set goocatch=false

catch (Exception e) {
               wenttocatch=false;
               System.out.println("xx");
            }

如果您做的比这里显示的多,请使用计数器[如果您的计数或布尔值,如果您不是],但除非您做的更多,否则请执行上面的第一件事

boolean wenttocatch;
int count = 0;

            do{


             try {
                 wenttocatch=false;
                number_of_rigons=sc.nextInt(); // sc is an object of scanner class 
            } catch (Exception e) {
               count++;
               wenttocatch=true;
               System.out.println("xx");

            }

            }while(wenttocatch==true && count < 1);

回复评论:

我认为您想获得整数,直到用户不再输入。 根据您的输入,一种方法是这样

int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
    //do something
}

您只需使用 hasNext(); java中的方法,而不是try and catch。 hasNext() 是 Java Scanner 类的一个方法,如果此扫描器的输入中有另一个标记,则返回 true。

 String[] stringArray = new String[lines];
   int i = 0;
   try (Scanner sc = new Scanner(file)) {
       while (sc.hasNextLine()) {
           String data=sc.nextLine();
           stringArray[i] = data;

           i++;
       }
   } catch (FileNotFoundException fileNotFoundException) {
       System.err.println("Error opening file.");
       throw new FileNotFoundException();
   } catch (NoSuchElementException e) {
       System.err.println("Error in file record structure");
       throw new NoSuchElementException();
   } catch (Exception e) {
       e.printStackTrace();
   }
  

/*在里面添加了扫描仪声明尝试我新的编程它对我有用,但我不知道它是否是好的做法*/:

    boolean wenttocatch;
    do 
    {
        try 
        {
           
        Scanner sc=new Scanner(System.in);
         wenttocoatch = false;
        number_of_rigons = sc.nextInt(); 
    } 
    catch (Exception e) 
    {
        wenttocatch=true;
        System.out.println("xx");
    }
} while (wenttocatch==true);
....
try {
  ....
} catch (Exception e) {
  sc.next();
  wenttocatch=true;
  System.out.println("xx");
}

暂无
暂无

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

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