Java错误“线程“main”中的异常java.util.InputMismatchException”在数组程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18407860/
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-08-12 01:21:39  来源:igfitidea点击:

Java Error "Exception in thread "main" java.util.InputMismatchException" On an Array program

javaarrays

提问by Nikhil Gopal

I recently typed out this java program to accept ten areas and their pin-codes and then search to find a particular area and print out it's pin-code. Here's the code from the program :

我最近输入了这个 java 程序来接受十个区域和它们的密码,然后搜索找到一个特定的区域并打印出它的密码。这是程序中的代码:

import java.util.Scanner;
public class Sal {

    public static void main (String args []){ 
        Scanner s=new Scanner(System.in);
        System.out.println("Enter 10 areas and their pincodes");
        String area[]=new String [10];
        int pincode[]=new int [10];
        String search;
        int chk=0;
        int p=0;

        for (int i=0;i<=9;i++){
            area[i]=s.nextLine();
            pincode[i]=s.nextInt();
        }

        System.out.println("Enter Search"); 
        search=s.nextLine();

        for (int j=0;j<=9;j++){
            if(search==area[j]){
                chk=1;
                j=p;
                break;
            }
        }

        if(chk==1){
            System.out.println("Search Found "+"Pincode : "+pincode[p] );
        } else {
            System.out.println("Search not Found");
        }
    }
}

And after entering two areas I get this ERROR:

进入两个区域后,我收到此错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Sal.main(Sal.java:14)

Can someone please tell me what I'm doing wrong! :/ Any help is appreciated.

有人可以告诉我我做错了什么!:/任何帮助表示赞赏。

采纳答案by Pranav Nandan

First of all, remember to indent your code for readability.

首先,请记住缩进代码以提高可读性。

Concept 1.

概念 1。

for (int i=0;i<=9;i++){

area[i]=s.next();// Use this for String Input

pincode[i]=s.nextInt();

s.nextLine();//Use this for going to next line of input

}

Concept 2.

概念 2。

if(search.compareTo(area[j])==0){ 

// compare Strings using compareTo method (which returns 0 if equal

// 使用 compareTo 方法比较字符串(如果相等则返回 0

Rest of your code and concepts are correct :)

您的其余代码和概念是正确的:)

回答by Eng.Fouad

From InputMismatchException's JavaDoc:

来自InputMismatchException 的 JavaDoc

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

由扫描器抛出以指示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

It seems that you entered a string whereas nextInt()expects an integer.

您似乎输入了一个字符串,而nextInt()期望输入一个整数。

回答by Xynariz

I'm assuming the error happens on the line pincode[i]=s.nextInt();(which is line 14). The reason this happens is because the input (from System.in) cannot be parsed as an int. Are you sure you're entering correct values?

我假设错误发生在线路上pincode[i]=s.nextInt();(即第 14 行)。发生这种情况的原因是因为输入(来自 System.in)无法解析为int. 你确定你输入了正确的值吗?

回答by Dennis Meng

From the docs for Scanner#nextInt():

从文档中Scanner#nextInt()

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

So, it sounds like your Scanneris trying to read in an intbut getting something that it can't turn into an int(either what it read is not a number or the number is too large).

因此,听起来您Scanner正在尝试读取一个int但得到它无法变成一个的int东西(它读取的不是数字或数字太大)。

You call the relevant function here:

您在此处调用相关函数:

for (int i=0;i<=9;i++){
    area[i]=s.nextLine();
    pincode[i]=s.nextInt(); // <-- the culprit
}

My guess is that at some point, your call to .nextLine()swallows up an entire line, and the next line starts with an "area". I can't do more without knowing how you expect the input to be formatted.

我的猜测是,在某些时候,您的调用会.nextLine()吞没整行,而下一行以“区域”开头。如果不知道您希望如何格式化输入,我就无能为力了。

回答by krstf

The input can not be parsed as an integer. Maybe you have a comma at the end of line.

输入不能被解析为整数。也许你在行尾有一个逗号。

btw:

顺便提一句:

if(search==area[j])

如果(搜索==区域[j])

is bad practice to check string equality. use search.equals(area[j]) with null-check.

检查字符串相等性是不好的做法。使用 search.equals(area[j]) 进行空检查。

回答by Gayatri Voora

I'm new to Programming. However, I faced the similar issue to over come while creating customers for bank (its just a practice problem). To overcome the problem I have created separate scanners for each input type & Closed all the scanners at the end of program. It worked.

我是编程新手。但是,我在为银行创建客户时遇到了类似的问题(这只是一个实践问题)。为了克服这个问题,我为每种输入类型创建了单独的扫描仪,并在程序结束时关闭了所有的扫描仪。有效。