gpt4 book ai didi

java - 用于解析输入的代码会抛出 InputMismatchException

转载 作者:行者123 更新时间:2023-12-02 08:05:42 27 4
gpt4 key购买 nike

while(inputbook.hasNext()){
id = inputbook.nextInt();
name = inputbook.next();
year = inputbook.nextInt();
price = inputbook.nextDouble();
Book b = new Book(id,name,year,price);
hmbook.put(id,b);
Book b2 = hmbook.get(id);
System.out.println(b2);

getCb3();
cb3.addItem(name);
}

这是文本文件:

100 JAVA 2007 100.0
200 C++ 2005 65.9
300 Introduction 2008 120.0

但是我遇到了 InputMismatch 异常:

Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at FirstFrame.loadFiles(FirstFrame.java:143)
at FirstFrame.<init>(FirstFrame.java:712)
at FirstFrame$7.run(FirstFrame.java:699)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是一个异常(exception)。为什么会发生这种情况?

最佳答案

您的语言环境可能希望 double 使用逗号作为小数点分隔符。你可以试试

Scanner inputbook = new Scanner(new File("book.txt"));
inputbook.useLocale(Locale.US);

看看这是否会改变什么。我还想补充一下

while(inputbook.hasNext()){
id = inputbook.nextInt();
System.out.println("id=" + id);
name = inputbook.next();
System.out.println("name=" + name);
year = inputbook.nextInt();
System.out.println("year=" + year);
price = inputbook.nextDouble();
System.out.println("price=" + price);

这样您就会知道上次成功读取的变量是什么,并且更容易确定出了什么问题。

关于java - 用于解析输入的代码会抛出 InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197591/

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