标签: inputmismatchexception

扫描仪双值 - InputMismatchException

我尝试以最简单的方式使用扫描仪:

码:

double gas, efficiency, distance, cost;
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of gallons of gas in the tank: ");
gas = scanner.nextDouble();
System.out.print("Enter the fuel efficiency: ");
efficiency = scanner.nextDouble();
Run Code Online (Sandbox Code Playgroud)

但在第一次输入后5.1它抛出:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at udacity.MileagePrinter.main(MileagePrinter.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)

JavaDoc中的状态:

由扫描程序抛出,表示检索到的令牌
与预期类型的模式不匹配,或者令牌超出预期类型的​​范围.

但在我看来,所有看起来都正确,并且应该工作正常.

问题:

  • 为什么会出现这种情况?
  • 如何规避这个麻烦?

java inputmismatchexception

21
推荐指数
2
解决办法
3万
查看次数

线程"main"java.util.InputMismatchException中的异常

我需要java中的一个练习的帮助,我可能会坚持这个错误2小时.任何帮助都会很棒.

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at prodavnica.Prodavnica.main(Prodavnica.java:60)
Java Result: 1


package prodavnica;

public class Proizvod {

    private String ime_proizvod;
    private static int cena;

    public Proizvod(String ime_proizvod, int cena) {
        this.ime_proizvod = ime_proizvod;
        this.cena=cena;
    }

    public String getIme_proizvod() {
        return ime_proizvod;
    }

    public void setIme_proizvod(String ime_proizvod) {
        this.ime_proizvod = ime_proizvod;
    }

    public static int getCena() {
        return cena;
    }

    public static void setCena(int cena) {
        Proizvod.cena = cena;
    }

    public void pecatiPodatoci(){ …
Run Code Online (Sandbox Code Playgroud)

java program-entry-point inputmismatchexception

7
推荐指数
1
解决办法
5万
查看次数

捕获异常后请求输入

我希望用户输入一个由以下代码扫描的数字:

scanner.nextInt();
Run Code Online (Sandbox Code Playgroud)

如果用户输入字符串,则程序会抛出InputMismatchException,这很明显.我想以这样的方式捕获异常,即程序提示用户输入输入,直到用户输入整数值.

Scanner scanner = new Scanner(System.in);
while(true) {
    try {
        System.out.println("Please enter a number: ");
        int input = scanner.nextInt();
        System.out.println(input);
        //statements
        break;
    }
    catch(InputMismatchException | NumberFormatException ex ) {
        continue;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果输入字符串,此代码将创建无限循环.

java java.util.scanner numberformatexception inputmismatchexception

6
推荐指数
1
解决办法
8440
查看次数

为什么我得到InputMismatchException?

到目前为止我有这个:

public double checkValueWithin(int min, int max) {
    double num;
    Scanner reader = new Scanner(System.in);
    num = reader.nextDouble();                         
    while (num < min || num > max) {                 
        System.out.print("Invalid. Re-enter number: "); 
        num = reader.nextDouble();                         
    }
    return num;
}
Run Code Online (Sandbox Code Playgroud)

还有这个:

public void askForMarks() {
    double marks[] = new double[student];
    int index = 0;
    Scanner reader = new Scanner(System.in);
    while (index < student) {
        System.out.print("Please enter a mark (0..30): ");
        marks[index] = (double) checkValueWithin(0, 30); 
        index++;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我测试这个,它不能采取双数,我得到这个消息:

Exception in thread …
Run Code Online (Sandbox Code Playgroud)

java input java.util.scanner inputmismatchexception

5
推荐指数
2
解决办法
13万
查看次数

Java:Do-While循环中的Try-Catch语句

我正在尝试执行一些扫描用户输入值的代码.这个动作包含在我写的自定义方法中,名为getTriangleDim(); 该方法读入users int值,确保它在某个范围内,然后返回输入的int值.该方法效果很好,我没有任何问题.

当我为getTriangleDim()方法输入非int值时出现问题.它给我一个InputMismatchException错误.我已经在do-while循环中编写了一个try-catch语句来尝试修复此问题.但这是我第一次使用try-catch语句,而我显然错过了一些东西.

以下是嵌套在do-while循环中的try-catch语句的代码:

//loop to scan for triangle dimension
        boolean bError = true;
        int triangle;

        do{
            try {
                triangle = getTriangleDim();
                bError=false;
                }
            catch (Exception e){
                System.out.println("You did not enter an integer, please enter an integer value");
                triangle = getTriangleDim();

                }
        }while (bError);
Run Code Online (Sandbox Code Playgroud)

如果我通过输入一个char值代替int来测试它,它实际捕获错误一次,然后打印我的"你没有....."语句.但是,如果我重新输入另一个非int数字,我再次得到一个运行时错误,说.......你猜对了........ InputMismatchException错误.

我的方法的代码在这里:

//method for scanning triangle dimensions from keyboard
    public static int getTriangleDim(){
        int triangle = 0;
        Scanner keyboard = new Scanner(System.in);
        do{
        System.out.print("Enter a non-zero integer length (+/-1 - +/-16): ");
        triangle = …
Run Code Online (Sandbox Code Playgroud)

java exception-handling try-catch do-while inputmismatchexception

5
推荐指数
1
解决办法
2万
查看次数

Java,使用 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);
Run Code Online (Sandbox Code Playgroud)

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

如何识别用户是否输入了一些非整数?
如果用户输入一个非整数,我如何要求用户再次输入?

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

java input try-catch java.util.scanner inputmismatchexception

5
推荐指数
2
解决办法
2万
查看次数

数字格式和输入不匹配异常有何不同

当在 Integer 类构造函数中传递字符值而不是整数值时,以下代码将引发NumberFormatException

class Wrap
{
    public static void main(String...args)
    {
        Integer j=new Integer("s");
        System.out.println(j);
    }
}
Run Code Online (Sandbox Code Playgroud)

当用户输入字符值而不是整数值时,以下代码将引发InputMismatchException

import java.util.Scanner;

class User
{
    public static void main(String...args)
    {
        Scanner obj=new Scanner(System.in);
        int i=obj.nextInt();
        int j=obj.nextInt();
        System.out.println("sum of numbers input by user");
        System.out.println(i+j);
    }
}
Run Code Online (Sandbox Code Playgroud)

这两个异常似乎都是在相同的场景中抛出的,那么它们有何不同呢?

java exception number-formatting java.util.scanner inputmismatchexception

2
推荐指数
1
解决办法
5410
查看次数

为什么我输入无效无限循环?

当初始选择无效时,为什么这会导致我陷入无限循环?

while (true) {
    System.out.print("Choice:\t");
    try {
        int choice = scanner.nextInt();
        break;
    } catch (InputMismatchException e) {
        System.out.println("Invalid Input!");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Choice: df
Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input!
Choice: Invalid Input! …
Run Code Online (Sandbox Code Playgroud)

java exception infinite-loop java.util.scanner inputmismatchexception

2
推荐指数
1
解决办法
283
查看次数

Java 扫描仪的问题:InputMismatchException

我有这个代码:

import java.util.Scanner;

public class Maggiore3Valori {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("System.in");

        int num1, num2, num3;
        int max;

        System.out.println("Inserisci il primo numero: ");
        num1 = scanner.nextInt();

        System.out.println("Inserisci il secondo numero: ");
        num2 = scanner.nextInt();

        System.out.println("Inserisci il terzo numero: ");
        num3 = scanner.nextInt();

        if (num1 > num2 && num1 > num3) {
            max = num1;
        } else if (num2 > num1 && num2 > num3) {
            max = num2;
        } else {
            max = num3; …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner inputmismatchexception

1
推荐指数
1
解决办法
119
查看次数

在While循环中尝试捕捉

下面的代码询问用户他/她想要多少赛车手.

while (true) { // loops forever until break
    try { // checks code for exceptions
        System.out.println("How many racers should" + " participate in the race?");
        amountRacers = in.nextInt();
        break; // if no exceptions breaks out of loop
    } 
    catch (InputMismatchException e) { // if an exception appears prints message below
        System.err.println("Please enter a number! " + e.getMessage());
        continue; // continues to loop if exception is found
    }
}
Run Code Online (Sandbox Code Playgroud)

如果在amoutnRacers = in.nextInt();代码中输入一个数字,则循环出来并且程序的其余部分运行正常; 但是,当我输入诸如"awredsf"之类的内容时,它应该捕获该异常,它会这样做.而不是再次提示用户它不断循环,这对我来说没有意义.

程序在连续循环时打印如下:

有多少参赛者应参加比赛?有多少参赛者应参加比赛?有多少参赛者应参加比赛?有多少参赛者应参加比赛?有多少参赛者应参加比赛?有多少参赛者应参加比赛?有多少赛车手参加比赛?请输入一个号码!null请输入一个数字!null请输入一个数字!null请输入一个数字!null请输入一个数字!null请输入一个数字!null请输入一个数字!空值 ...

我不明白发生了什么amountRacers …

java exception-handling try-catch while-loop inputmismatchexception

0
推荐指数
1
解决办法
2万
查看次数