这个错误消息的含义是什么,我该如何解决这个问题?

huangapple go评论82阅读模式
英文:

What does this error message mean and how do i solve the issue?

问题

import java.util.*;

class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        String c = sc.next();
        String d = sc.next();
        String e = sc.next();
        String f = sc.next();
        String g = sc.next();
        String h = sc.next();
        String i = sc.next();
        String j = sc.next();
        String k = sc.next();
        String l = sc.next();
        String m = sc.next();
        String n = sc.next();
        String o = sc.next();
        String p = sc.next();
        
        System.out.println(h);
        System.out.println(g);
        System.out.println(f);
        System.out.println(e);
        System.out.println(d);
        System.out.println(c);
        System.out.println(b);
        System.out.println(a);
        System.out.println(p);
        System.out.println(o);
        System.out.println(n);
        System.out.println(m);
        System.out.println(l);
        System.out.println(k);
        System.out.println(j);
        System.out.println(i);
    }
}

错误信息:

Failed test #1 of 3. Runtime error
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.main(Main.java:15)

希望这些翻译对您有所帮助。如果您有任何其他问题,请随时问我。

英文:

I started learning java very recently and am pretty confused with scanners and input in general. For this example, my output is exactly what it needs to be but I get an odd error message, what does this mean and what is my mistake?

The code, sorry if it looks terribly unorganized but it's my first time with something this long.

import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
String c = sc.next();
String d = sc.next();
String e = sc.next();
String f = sc.next();
String g = sc.next();
String h = sc.next();
String i = sc.next();
String j = sc.next();
String k = sc.next();
String l = sc.next();
String m = sc.next();
String n = sc.next();
String o = sc.next();
String p = sc.next();
System.out.println(h);
System.out.println(g);
System.out.println(f);
System.out.println(e);
System.out.println(d);
System.out.println(c);
System.out.println(b);
System.out.println(a);
System.out.println(p);
System.out.println(o);
System.out.println(n);
System.out.println(m);
System.out.println(l);
System.out.println(k);
System.out.println(j);
System.out.println(i);
}
}

The error message:

Failed test #1 of 3. Runtime error
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.main(Main.java:15)

答案1

得分: 2

在测试1中,可能没有任何元素传递到您程序中的那个点。

您应该检查在访问下一个元素之前调用hasNext()方法:

if (sc.hasNext()) {
    value = sc.next();
}

来自官方Java 文档

抛出:
NoSuchElementException - 如果没有更多的标记可用

英文:

In the test #1 of 3, perhaps no elements are passed to your program in that point.

You should check, calling the hasNext() method before accessing the next element:

if (sc.hasNext()) {
value = sc.next();
}

From the official Java documentation:

Throws:
NoSuchElementException - if no more tokens are available

答案2

得分: 0

这个异常意味着在调用sc.next()方法时,Scanner从输入中没有可读取的值。

因此,您尝试读取的项数比输入流中实际有的多。
Scanner有一个hasNext()方法,它返回一个布尔值,指示流中是否还有要读取的内容。

您可以像这样重写您的示例:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
       System.out.println(sc.next());
    }
}

这段代码会读取与输入流中可用值数量相同的值,并将它们打印到控制台上。

英文:

This exception means, that there is no value to read for the Scanner from the input on invoking sc.next() method.

So, you are trying to read more items then input stream has.
Scanner has method hasNext() which returns boolean to indicate if there still something to read in the stream.

You can rewrite your example, like this:

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
System.out.println(sc.next());
}
}

This code will read as many values as your input stream has and print them to the console.

huangapple
  • 本文由 发表于 2020年4月10日 20:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定