Good morning,
I am very new to Java and I tried to create a program that might run a OK.
I created the codes and it worked fine. Only problem I have -> is the error trapping session. When I entered invalid characters, aside from numbers, it popped up with the warning. Then, I could not loop back to the beginning of the program.
I might forget something simple.
There should not be any additional class. One class should be enough for this one..
Here are the codes:
import java.text.*;
import javax.swing.*;
public class ScoreTest {
public static void main(String[] args) {
double AverageScore = 0.000, SumScore = 0.0, sd, BestScore = 0.0;
String str1 = "";
String str2 = "";
String str3 = "";
int number_of_time = 1;
try{
while(!(str1.equalsIgnoreCase("x")))
{
str1 = JOptionPane.showInputDialog("Enter test score:");
sd = Double.parseDouble(str1);
if(number_of_time == 1){
BestScore = sd;
SumScore = sd;}
else{
SumScore=SumScore+sd;
if (sd > BestScore){
BestScore = sd;
}
}
AverageScore = SumScore/number_of_time;
DecimalFormat formatter = new DecimalFormat ("00.000");
str2 = JOptionPane.showInputDialog(null,"Please Enter to continue, \n or 'x' to view test data results.");
if((str2.equalsIgnoreCase("x"))){
str3 = JOptionPane.showInputDialog(null, "Number of scores: " + number_of_time + "\n" + "Average Score: " + formatter.format(AverageScore)+ "\n" + "Best score: " + BestScore + "\n" + "Press Enter to continue entering test scores, \n or 'x' to exit.");
if((str3.equalsIgnoreCase("x"))){
System.exit(0);
}//end str3
}//end of Str2
number_of_time +=1;
}//end of while loop
}//end of try
catch(NumberFormatException e){
str1 = JOptionPane.showInputDialog(null, "Invalid Entry" + "\n"
+ "Press OK to Continue to Enter a Valid Test Score. ", "Invalid Input", JOptionPane.ERROR_MESSAGE);
sd = Double.parseDouble(str1);
}
}
}
************************************************************************
What I would like to do:
1. When the error information screen popped up, I would like to press OK. Then, the program should loop back to where the error starts. If I have the count of three times before the error occurs, those three times should still be there, including the best score and average. The system should not count the time that the error occurs.
2. I think the problem lies on the last part because I do not know what to do, in order to loop back. It starts after " catch(NumberFormatException e){" section.
I am unsure whether I should create any new variables.
Please do not recommend creating a new class. I believe that it can work here with one class, unless we do not have any other options.
Thank you again for your help. I am new to this and try my best to make it to work.
I am very new to Java and I tried to create a program that might run a OK.
I created the codes and it worked fine. Only problem I have -> is the error trapping session. When I entered invalid characters, aside from numbers, it popped up with the warning. Then, I could not loop back to the beginning of the program.
I might forget something simple.
There should not be any additional class. One class should be enough for this one..
Here are the codes:
import java.text.*;
import javax.swing.*;
public class ScoreTest {
public static void main(String[] args) {
double AverageScore = 0.000, SumScore = 0.0, sd, BestScore = 0.0;
String str1 = "";
String str2 = "";
String str3 = "";
int number_of_time = 1;
try{
while(!(str1.equalsIgnoreCase("x")))
{
str1 = JOptionPane.showInputDialog("Enter test score:");
sd = Double.parseDouble(str1);
if(number_of_time == 1){
BestScore = sd;
SumScore = sd;}
else{
SumScore=SumScore+sd;
if (sd > BestScore){
BestScore = sd;
}
}
AverageScore = SumScore/number_of_time;
DecimalFormat formatter = new DecimalFormat ("00.000");
str2 = JOptionPane.showInputDialog(null,"Please Enter to continue, \n or 'x' to view test data results.");
if((str2.equalsIgnoreCase("x"))){
str3 = JOptionPane.showInputDialog(null, "Number of scores: " + number_of_time + "\n" + "Average Score: " + formatter.format(AverageScore)+ "\n" + "Best score: " + BestScore + "\n" + "Press Enter to continue entering test scores, \n or 'x' to exit.");
if((str3.equalsIgnoreCase("x"))){
System.exit(0);
}//end str3
}//end of Str2
number_of_time +=1;
}//end of while loop
}//end of try
catch(NumberFormatException e){
str1 = JOptionPane.showInputDialog(null, "Invalid Entry" + "\n"
+ "Press OK to Continue to Enter a Valid Test Score. ", "Invalid Input", JOptionPane.ERROR_MESSAGE);
sd = Double.parseDouble(str1);
}
}
}
************************************************************************
What I would like to do:
1. When the error information screen popped up, I would like to press OK. Then, the program should loop back to where the error starts. If I have the count of three times before the error occurs, those three times should still be there, including the best score and average. The system should not count the time that the error occurs.
2. I think the problem lies on the last part because I do not know what to do, in order to loop back. It starts after " catch(NumberFormatException e){" section.
I am unsure whether I should create any new variables.
Please do not recommend creating a new class. I believe that it can work here with one class, unless we do not have any other options.
Thank you again for your help. I am new to this and try my best to make it to work.