如何在未定义数组长度的Java中输入?

发布于 2025-02-13 09:10:45 字数 855 浏览 3 评论 0 原文

我的输入是这种格式:

1 2 3 4 5 6
Alice

阵列长度尚不清楚。 我以这种方式进行了编码:

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int i=0;
        while(sc.hasNext()){
            arr.add(sc.nextInt());
        }
        String player = sc.nextLine();
    }
}

但是我遇到了这个错误。

Exception in thread "main" java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:939)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Main.main(Main.java:17)

提前致谢。

My input is in this format:

1 2 3 4 5 6
Alice

The array length is not known.
I coded it this way:

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int i=0;
        while(sc.hasNext()){
            arr.add(sc.nextInt());
        }
        String player = sc.nextLine();
    }
}

But I am getting this error.

Exception in thread "main" java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:939)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Main.main(Main.java:17)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-02-20 09:10:45

您看到 java.util.inputMismatchException 是因为您向语句 sc.nextint()提供了输入“ alice” 't知道如何将 java.lang.string 输入转换为int(这是 next> nextint() )。

这是一个非常简单的示例,可以重现相同的行为:

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

如果您运行这两行并输入非授权程序,例如“ d”,它将引发一个例外:

d
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at a91.main(a91.java:6)

要修复它,您需要替换 nextint() nextint()< /code>具有耐受性输入的东西,可能 nextline() 。这是一个(非常)简单的示例,显示如何工作。请注意,这只是突出显示您要问的行为,即:如何解决 inputMismatchException 。与您的原始程序一样,没有循环终止 - 它将永远运行(直到您退出程序)。

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

while (sc.hasNext()) {
    String s = sc.nextLine();
    System.out.println("got this: " + s);
}

1 2 3 4 5 6
got this:  2 3 4 5 6
Alice
got this: Alice

The reason you are seeing java.util.InputMismatchException is because you provided input "Alice" to the statement sc.nextInt(), and the scanner is telling you that it doesn't know how to convert a java.lang.String input to an int (which is the return type of nextInt()).

Here's a very simple example that reproduces the same behavior:

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

If you run those two lines and enter a non-integer, like "d", it will throw an exception:

d
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at a91.main(a91.java:6)

To fix it, you need to replace nextInt() with something that is tolerant of non-numeric input, possibly nextLine(). Here is a (very) simple example showing how that could work. Note this is just highlighting the behavior you're asking about, namely: how to address InputMismatchException. As with your original program, there is no loop termination – it will run forever (until you quit the program).

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

while (sc.hasNext()) {
    String s = sc.nextLine();
    System.out.println("got this: " + s);
}

1 2 3 4 5 6
got this:  2 3 4 5 6
Alice
got this: Alice
笔芯 2025-02-20 09:10:45

您应该使用 hasnextint 检查整数输入。一旦不再是整数,只需使用 next()读取播放器即可。

List<Integer> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);

while(sc.hasNextInt()){
    arr.add(sc.nextInt());
}
String player = sc.next();

arr.forEach(System.out::println);
System.out.println(player);

示例输入的支持

10 20 30 40 50 60 70
Alice

10 20 30 40
50 60 70 Alice

10 20 30
40
50
60 70 Alice

10 20 30
40 50
60 70
Alice

输出

10
20
30
40
50
60
70
Alice

You should use hasNextInt to check for integer input. Once no more integers, then just use next() to read the player.

List<Integer> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);

while(sc.hasNextInt()){
    arr.add(sc.nextInt());
}
String player = sc.next();

arr.forEach(System.out::println);
System.out.println(player);

Example input's supported

10 20 30 40 50 60 70
Alice

10 20 30 40
50 60 70 Alice

10 20 30
40
50
60 70 Alice

10 20 30
40 50
60 70
Alice

output

10
20
30
40
50
60
70
Alice
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文