I wrote a java program that asks the user to enter the name of the student and the GPA. It contains 3 buttons:
- save: to save name and GPA in 2 arrays.
- clear: to clear text field.
- exit: to make everything invisible and the show student name with max GPA, and number of students
the maximum number of students is 100 and GPA must be 4 or below and when it is above 4 or below 0, an invalid message should appear.
but unfortunately, the exit button is not working and it gives me the following error:
Exception in thread "AWT-EventQueue-1" 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 StudentGPA.actionPerformed(StudentGPA.java:67) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
when the text field is empty the program gives me an error, but when it is not empty it makes everything invisible
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentGPA extends JApplet implements ActionListener{
JTextField t1,t2;
JLabel l1,l2,l3,l4,invalid,max;
Container cp;
JPanel p1,p2,p3,p4,p5;
JButton saveB,clearB,exitB;
public void init()
{
cp= getContentPane();
cp.setLayout(new GridLayout(5,1));
p1= new JPanel(new FlowLayout(FlowLayout.LEFT));
l1= new JLabel("Applet");
p1.add(l1);
cp.add(p1);
p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
l2= new JLabel("Enter students name and GPA: ");
p2.add(l2);
cp.add(p2);
p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
l3= new JLabel("Name: ");
t1 = new JTextField(20);
p3.add(l3);
p3.add(t1);
cp.add(p3);
p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
l4= new JLabel("GPA: ");
invalid = new JLabel("Invalid GPA!");
invalid.setVisible(false);
t2 = new JTextField(15);
p4.add(l4);
p4.add(t2);
p4.add(invalid);
cp.add(p4);
p5 = new JPanel(new FlowLayout(FlowLayout.CENTER));
saveB = new JButton("Save");
saveB.addActionListener(this);
clearB = new JButton("Clear");
clearB.addActionListener(this);
exitB = new JButton("Exit");
exitB.addActionListener(this);
p5.add(saveB);
p5.add(clearB);
p5.add(exitB);
cp.add(p5);
}
public void actionPerformed(ActionEvent e) {
String ac=e.getActionCommand();
String[] nArray = new String[100];
double[] gArray = new double[100];
String name=String.valueOf(t1.getText());
double gpa=Double.parseDouble(t2.getText());
double maxGPA=0;
String maxName=null;
int counter=0;
if(ac.equals("Save") && gpa>=0 && gpa<=4&&counter<100)
{
for(int i=0;i<100;i++)
{
nArray[i]=name;
gArray[i]=gpa;
t1.setText(null);
t2.setText(null);
invalid.setVisible(false);
if(gpa>maxGPA)
{
maxGPA=gpa;
maxName=name;
}
counter++;
}
}
else
{
invalid.setVisible(true);
}
if(ac.equals("Clear"))
{
t1.setText(null);
t2.setText(null);
invalid.setVisible(false);
}
else if(ac.equals("Exit"))
{
this.setVisible(false);
l2.setText(maxName+" scored the maximum GPA ("+maxGPA+")\namong a total of "+counter+" students");
l2.setVisible(true);
}
}
}
I expect when I click the exit button the name of max GPA student and number of students