gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:48:19 24 4
gpt4 key购买 nike

以下 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 中的标记。 String 由空格分隔符标记,因此标记如下:

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

while(s.hasNext()){
System.out.println(s.next());
}
//Outputs: Hello
// 12345

错误消息只是告诉您它无法在标记中找到 hello[space],即 hello12345

如果您想查找模式而不管使用什么分隔符,String#findInLine :

s.findInLine("hello ");

关于Java 输入不匹配异常错误。是什么原因造成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24465225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com