null
线程“awt-eventqueue-0”java.lang.NumberFormatException中出现异常:在sun.misc.floatingDecimal.ReadJavaFormatString(FloatingDecimal.Java:1842)在sun.misc.floatingDecimal.ParseDouble(FloatingDecimal.Java:110)在java.lang.Double.ParseDouble(Double.Java:538)在coursework.coursework.ActionPerformed(Coursework.Java:135)在在java.awt.Container.DispatchEventiMPL(component.Java:4881)在java.awt.Component.DispatchEvent(component.Java:4703)在java.awt.LightWeightDispatcher.RetargetMouseEvent(container.Java:4898)在java.awt.LightWeightDispatcher.ProcessMouseEvent(container.Java:4533)在java.awt.LightWeightDispatcher.ProcessMouseEvent(container.Java:4533)在java.awt.EventQueue4.run(EventQueue.java:724)在java.awt.EventQueue4.run(EventQueue.java:722)在java.security.ProtectionDomain1.doIntersectionPrivileged(Native Method)在java.security.ProtectionDomain1.doIntersectionPrivilege(ProtectionDomain.java:75)在java.awt.EventQuee.DispatchEvent(EventQueue.java:721)在
和代码
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.coursework2.ActionPerformed(Coursework.java:135)
在尝试将字符串转换为数字时,您似乎遇到了NumberFormatException。
您有两个选项,在尝试在第136-139行转换字符串之前,您可以验证字符串是否包含有效的数字。或者确保第133-135行中的输入包含有效的数字。
记住,空字符串不是有效的数字,这是一个非常容易执行的验证。
与其试图确定什么是转换为数字的有效字符串,不如让Java为您计算。捕获异常并执行特定应用程序可接受的操作。在您的示例中,并不是字符串中有无效字符,而是令牌有一个空字符串“”。
double g1 = 0;
try {
g1 = Double.parseDouble(token[3]);
catch (NumberFormatException e) {
System.out.println(e.getMessage() + ": " + token[3]);
}