Java输入不匹配异常错误。是什么原因造成的?

Java输入不匹配异常错误。是什么原因造成的?,java,java.util.scanner,inputmismatchexception,Java,Java.util.scanner,Inputmismatchexception,以下Java过程导致InputMismatchException错误的原因是什么 import java.util.Scanner; public class Main { public static void main(String[] args) { String input = "hello 12345\n"; Scanner s = new Scanner(input); s.next("hello "); } } 谢谢之

以下Java过程导致InputMismatchException错误的原因是什么

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String input = "hello 12345\n";
        Scanner s = new Scanner(input);
        s.next("hello ");
    }
}

谢谢

之所以发生这种情况,是因为
hello[space]
不是
字符串中的标记。
字符串
由空格分隔符标记,因此标记如下所示:

String input = "hello 12345\n";
Scanner s = new Scanner(input);

while(s.hasNext()){
    System.out.println(s.next());
}
//Outputs: Hello
//         12345
错误消息只是告诉您在令牌中找不到
hello[space]
,这些令牌是
hello
12345

如果要查找模式而不考虑分隔符,请使用:


思考next()方法的作用。你期待什么?@Edward M.B.我希望它能在要扫描的字符串开头找到“hello”模式。@FrancescoRiccio太棒了!我最喜欢的开始早晨的方式,帮助别人编写代码!谢谢。
s.findInLine("hello ");