【发布时间】:2014-07-29 06:57:37
【问题描述】:
我正在努力为我的 Programm 争取一点高分。该程序有一个日志文件,我想读取该文件并确定获胜者。因为我的程序有一些问题,你可以一遍又一遍地回答同一个问题,并因此获得学分。 使用下面的代码,我试图防止作弊分数。我试图比较第三、第四和第五个字符串,它代表你必须解决的计算。当下一行具有相同的计算时,我想忽略高分的计数 有人可以帮我弄这个吗 ?我只是得到一堆错误。
感谢您的时间和帮助。
public int readFile() {
String score = null;
while (scan.hasNext()) {
String a = scan.next();
String b = scan.next();
String c = scan.next();
String d = scan.next();
String e = scan.next();
String f = scan.next();
String g = scan.next();
String h = scan.next();
String i = scan.next();
if (c.equals(scan.next()) && d.equals(scan.next()) && e.equals(scan.next())) {
} else {
temp = Integer.parseInt(i);
if (temp > highscore) {
highscore = temp;
}
}
}
return highscore;
}
文本文件:
014/07/29 08:25:15 95 + 80 4 false -171 0
2014/07/29 08:25:15 95 + 80 4 false -171 0
2014/07/29 08:25:49 8 * 3 24 true 0 1
2014/07/29 08:25:49 8 * 3 24 true 0 2
2014/07/29 08:25:49 8 * 3 24 true 0 3
2014/07/29 08:25:50 8 * 3 24 true 0 4
2014/07/29 08:25:50 8 * 3 24 true 0 5
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2014/07/28"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at mathe.Highscore.readFile(Highscore.java:42)
at mathe.Gui$TestActionListener.actionPerformed(Gui.java:231)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
【问题讨论】:
-
因为 OP 说你可以一遍又一遍地回答同一个问题,并为此获得学分并且没有提供任何堆栈跟踪
-
我只是得到一堆错误:不,你没有。您收到的每个错误都会附带一条准确的消息,说明问题所在和位置。不阅读它们,不发布它们是错误的。
-
重新格式化您的数据文件;条目之间只有一个空格。然后,使用
String[] entry = scan.nextLine().split(" ");... 这样您就可以以entry[2], [3], ...的身份访问计算,以同样的方式提前阅读。