Reputation: 191
I tried to develop a java program using netbeans in which a GUI accepts subject marks from five text fields and displays the total marks, percentage and the grade in their respective text fields. The problem is I am getting errors while executing the GUI. I tried replacing int with double to hold decimal values of percentage but that didn't help. I am unable to find any errors and since I am a beginner i am unable to understand the errors given in the monitor by my netbeans. Please help.
ERRORS: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 34"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at NewJFrame.submit_buttonActionPerformed(NewJFrame.java:173)
at NewJFrame.access$000(NewJFrame.java:4)
at NewJFrame$1.actionPerformed(NewJFrame.java:60)
Here's the coding I've done.
int sub1_ = Integer.parseInt(sub1.getText());
int sub2_ = Integer.parseInt(sub2.getText());
int sub3_ = Integer.parseInt(sub3.getText());
int sub4_ = Integer.parseInt(sub4.getText());
int sub5_ = Integer.parseInt(sub5.getText());
// Here each subject holds a max. of 100 marks.
int a = sub1_+sub2_+sub3_+sub4_+sub5_;
total_marks.setText(""+a);
// Since each subject holds a max. of 100 marks, the total marks of five subjects sums up to 500.
int b = (a/500)*100;
percentage.setText(b+"%");
if(b<=100&&b>=91)
{grade.setText("A1");}
else if(b<=90&&b>=81)
{grade.setText("A2");}
else if(b<=80&&b>=71)
{grade.setText("B1");}
else if(b<=70&&b>=61)
{grade.setText("B2");}
else if(b<=60&&b>=51)
{grade.setText("C1");}
else if(b<=50&&b>=35)
{grade.setText("C2");}
else if(b<=34&&b>=0)
{grade.setText("D");}
else {grade.setText("");}
Upvotes: 0
Views: 320
Reputation: 1155
In addition to the question asked, there is a way to reduce the clutter in your code.
For example, you have multiple if-statements that all do the same thing, but with different numbers. In this case, you can just create a function.
private boolean inRangeOf(int value, int max, int min) {
return value <= max && value >= min;
}
You can then replace your conditions with calls to inRangeOf(x, a, b)
if( inRangeOf(b, 100, 91) ) {
grade.setText("A1");
}
else if( inRangeOf(b, 90, 81) ) {
grade.setText("A2");
}
else if( inRangeOf(b, 80, 71) ) {
grade.setText("B1");
}
Upvotes: 0
Reputation: 26185
In addition to the issues already reported, the line int b = (a/500)*100;
should be int b = (a * 100) / 500;
or, simpler, int b = a/5;
. The division by 500 rounds down to an integer, so if a
is less than 500 the result is zero.
Upvotes: 2
Reputation: 19824
At some point you are trying to parse the integer " 34", the leading space is a problem for the parsing method.
You should either make sure your integer has no whitespace when you create it, or use the trim() function to remove leading and trailing whitespace,
Upvotes: 2
Reputation: 1155
java.lang.NumberFormatException: For input string: " 34"
Trim the String
of whitespace before using the parse methods.
try {
int sub1_ = Integer.parseInt(sub1.getText().trim());
int sub2_ = Integer.parseInt(sub2.getText().trim());
int sub3_ = Integer.parseInt(sub3.getText().trim());
int sub4_ = Integer.parseInt(sub4.getText().trim());
int sub5_ = Integer.parseInt(sub5.getText().trim());
} catch (NumberFormatException e) {
// invalid input
}
Upvotes: 5