Java:如何读取输入 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14259508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:38:59  来源:igfitidea点击:

Java: how to read an input int

javainputjava.util.scanner

提问by Marco

So, I was looking for an efficient way, using Java's standard packages, to read an input integer... For example, I came across the class "Scanner", but I found two main difficulties:

所以,我正在寻找一种有效的方法,使用 Java 的标准包来读取输入整数......例如,我遇到了“扫描仪”类,但我发现了两个主要困难:

  1. if I don't insert an int, I'm not actually able to solve the exception;
  2. this class works with tokens, but my aim is to load the string in its full length.
  1. 如果我不插入 int,我实际上无法解决异常;
  2. 这个类使用令牌,但我的目标是加载完整长度的字符串。

This is an example of execution I would like to realize:

这是我想实现的执行示例:

Integer: eight
Input error - Invalid value for an int.
Reinsert: 8 secondtoken
Input error - Invalid value for an int.
Reinsert: 8
8 + 7 = 15

And this is the (incorrect) code I tried to implement:

这是我试图实现的(不正确的)代码:

import java.util.Scanner;
import java.util.InputMismatchException;

class ReadInt{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        boolean check;
        int i = 0;
        System.out.print("Integer: ");
        do{
            check = true;
            try{
                i = in.nextInt();
            } catch (InputMismatchException e){
                System.err.println("Input error - Invalid value for an int.");
                System.out.print("Reinsert: ");
                check = false;
            }
        } while (!check);
        System.out.print(i + " + 7 = " + (i+7));
    }
}

采纳答案by xagyg

Use a BufferedReader. Check NumberFormatException. Otherwise very similar to what you have. Like so ...

使用 BufferedReader。检查 NumberFormatException。否则与您拥有的非常相似。像这样...

import java.io.*;

public class ReadInt{
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        boolean check;
        int i = 0;
        System.out.print("Integer: ");
        do{
            check = true;
            try{
                i = Integer.parseInt(in.readLine());
            } catch (NumberFormatException e){
                System.err.println("Input error - Invalid value for an int.");
                System.out.print("Reinsert: ");
                check = false;
            }
        } while (!check);
        System.out.print(i + " + 7 = " + (i+7));
    }
}

回答by Doorknob

To use with tokens:

与令牌一起使用:

int i = Integer.parseInt(in.next());

Then you could do:

那么你可以这样做:

int i;
while (true) {
    System.out.print("Enter a number: ");
    try {
        i = Integer.parseInt(in.next());
        break;
    } catch (NumberFormatException e) {
        System.out.println("Not a valid number");
    }
}
//do stuff with i

That above code works with tokens.

上面的代码适用于令牌。