gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"NumberFormatException : empty String 中出现异常

转载 作者:行者123 更新时间:2023-11-30 03:28:11 26 4
gpt4 key购买 nike

我的 GUI 有问题。代码尚未完成,但应该仍然可以工作。按读取按钮后出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at coursework.Coursework$2.actionPerformed(Coursework.java:135) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6525) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:702) at java.awt.EventQueue$3.run(EventQueue.java:696) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:724) at java.awt.EventQueue$4.run(EventQueue.java:722) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:721) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

和代码

package coursework;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Coursework extends JFrame {
public static void main(String[] args) {
JFrame frame=new JFrame("myframe");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(18,1));
JLabel StudentNameLabel = new JLabel("Student Name: ");
final JTextField TypeNameArea = new JTextField(" ");
JLabel StudentSurnameLabel = new JLabel("Student Surname: ");
final JTextField TypeSurnameArea = new JTextField(" ");
JLabel StudentIDLabel = new JLabel("Student ID: ");
final JTextField StudentIDArea = new JTextField("M00");
JLabel LabTestLabel = new JLabel("Lab Test: ");
final JTextField LabTestArea = new JTextField(" ");
JLabel QuizLabel = new JLabel("Quizes: ");
final JTextField QuizArea = new JTextField(" ");
JLabel ICourseworkLabel = new JLabel("Indiwidual CW: ");
final JTextField ICourseworkArea = new JTextField(" ");
JLabel GCourseworkLabel = new JLabel("Group CW: ");
final JTextField GCourseworkArea = new JTextField(" ");
panel.setBackground(Color.BLACK);
StudentSurnameLabel.setForeground(Color.YELLOW);
StudentNameLabel.setForeground(Color.YELLOW);
LabTestLabel.setForeground(Color.YELLOW);
StudentIDLabel.setForeground(Color.YELLOW);
QuizLabel.setForeground(Color.YELLOW);
ICourseworkLabel.setForeground(Color.YELLOW);
GCourseworkLabel.setForeground(Color.YELLOW);
JButton buttonSubmit = new JButton("Submit");
buttonSubmit.setBackground(Color.BLACK);
buttonSubmit.setForeground(Color.WHITE);
JButton buttonRead = new JButton("Read");
buttonRead.setBackground(Color.BLACK);
buttonRead.setForeground(Color.WHITE);
JButton buttonCancel = new JButton("Cancel");
buttonCancel.setBackground(Color.BLACK);
buttonCancel.setForeground(Color.WHITE);
JButton buttonNext = new JButton("Next");
buttonNext.setBackground(Color.BLACK);
buttonNext.setForeground(Color.WHITE);

panel.add(StudentNameLabel);
panel.add(TypeNameArea);
panel.add(StudentSurnameLabel);
panel.add(TypeSurnameArea);
panel.add(StudentIDLabel);
panel.add(StudentIDArea);
panel.add(LabTestLabel);
panel.add(LabTestArea);
panel.add(QuizLabel);
panel.add(QuizArea);
panel.add(ICourseworkLabel);
panel.add(ICourseworkArea);
panel.add(GCourseworkLabel);
panel.add(GCourseworkArea);
panel.add(buttonSubmit);
panel.add(buttonRead);
panel.add(buttonCancel);
panel.add(buttonNext);
frame.setSize(300, 800);
frame.add(panel);
frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



buttonNext.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e)
{
String name;
String surname;
String number;
String gradeLabTest;
String gradeQuiz;
String gradeICourswork;
String gradeGCourswork;
name = TypeNameArea.getText();
surname = TypeSurnameArea.getText();
number = StudentIDArea.getText();
gradeLabTest = LabTestArea.getText();
gradeQuiz = QuizArea.getText();
gradeICourswork = ICourseworkArea.getText();
gradeGCourswork = GCourseworkArea.getText();
String line = name + " " + surname + " " + number + " " + gradeLabTest + " " + gradeQuiz + gradeICourswork + gradeGCourswork + "\r\n";
Writer output;
try {
output = new BufferedWriter(new FileWriter("C:/Hola/test.txt",true)); //clears file every time
output.append(line);
output.close();
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

buttonRead.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e)
{
BufferedReader br ;
try {
br = new BufferedReader(new FileReader("C:/Hola/test.txt"));
for(String line; (line = br.readLine()) != null; ) {
String[] token = line.split(" ");
String name = token[0];
String surname = token[1];
String number = token[2];
double g1 = Double.parseDouble(token[3]);
double g2 = Double.parseDouble(token[4]);
double g3 = Double.parseDouble(token[5]);
double g4 = Double.parseDouble(token[6]);

double av = 0.4*g1 + 0.2*g2 + 0.2*g3 + 0.2*g4;
int grade = 0;
String gradestring;




String line_input = name + " " + surname + " " + number + " " + av + " " + grade + "\r\n";
Writer output;
try {
output = new BufferedWriter(new FileWriter("C:/Hola/test2.txt",true)); //clears file every time
output.append(line_input);
output.close();
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}


// line is not visible here.
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}

}
});
}

}

最佳答案

当您遇到错误时,只需按照 StackTrace 并查看包含您创建的类的第一行,在本例中:

coursework.Coursework$2.actionPerformed(Coursework.java:135)

尝试将字符串转换为数字时,您似乎遇到了 NumberFormatException。

您有两个选择,您可以在第 136-139 行尝试转换 String 之前验证它是否包含有效数字。或者确保第 133-135 行中的输入包含有效数字。

请记住,空字符串不是有效数字,这是您可以执行的非常简单的验证。

关于java - 线程 "AWT-EventQueue-0"NumberFormatException : empty String 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687198/

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