I have a condition where if the user inputs a negative number or a number which is more than 100, or a string, an error message should be printed "That wasn't a valid percentage, I need a number between 0-100. Try again." and ask the user to reenter a valid number. and if the user decided to just enter, all the input should be calculated and printed the average amount.
public static void main(String[ ] args) {
int count = 0; //count to stop loop
double[ ] aGrade = new double[SIZE];
String input = new String("");
Scanner scan = new Scanner(System.in);
double total = 0;
int gTotal = aGrade.length;
boolean exit = false;
while ((count < SIZE) && (!exit)) {
System.out.print("Enter number " (count 1) ": " "\n");
try {
input = scan.nextLine();
if (Double.parseDouble(input) > 0 && Double.parseDouble(input) < 100) {
aGrade[count] = Double.parseDouble(input); //put into the array
count ; //only increment count if success
} else
System.out.println("That wasn't a valid percentage,"
" I need a number between 0-100. Try again.");
} catch (NumberFormatException nfe) {
exit = true; //exit loop
}
}
System.out.println("number of grades entered: " count "\n");
for (int i = 0; i < count; i ) {
// print entered grade
System.out.println("grade " (i 1) ": " aGrade[i]);
}
for (int i = 0; i < count; i ) {
total = aGrade[i];
}
// calculate and print the average
System.out.println("\n" "Average grade: " total /count);
But when I run my code, if I input letters, it won't allow the user to reinput value but prints whatever is calculated. I think it is in my if-else statement, but I am not sure how
CodePudding user response:
Try something like this:
boolean ok = false;
try {
input = scan.nextLine();
if ("".equals(input)) {
ok = true;
exit = true;
} else if (Double.parseDouble(input) >= 0 && Double.parseDouble(input) <= 100) {
aGrade[count] = Double.parseDouble(input); //put into the array
count ; //only increment count if success
ok = true;
}
} catch (NumberFormatException nfe) {
// nothing
}
if (!ok) {
System.out.println("That wasn't a valid percentage,"
" I need a number between 0-100. Try again.");
}
CodePudding user response:
If you type letter as an input, you will never end up in your else
part of the if statement since code inside if
throws an exception and you are then inside catch
part. Also, you wrote inside catch
part, when NumberFormatException
happens(when you enter letter instead of number), set exit
to true
and that is the reason why program don't let you type again after you input letter. Fix those things and it will work. Also, take a look at how to debug your program, learn that skill, it will help you to solve this kind of problems in the future.
CodePudding user response:
''Double.parseDouble'' indicates to convert string to double
An NumberFormatException
exception is raised when you enter a string that cannot be parsed, so this row exit = true;
in your code will be executed:
/**
* Returns a new {@code double} initialized to the value
* represented by the specified {@code String}, as performed
* by the {@code valueOf} method of class
* {@code Double}.
*
* @param s the string to be parsed.
* @return the {@code double} value represented by the string
* argument.
* @throws NullPointerException if the string is null
* @throws NumberFormatException if the string does not contain
* a parsable {@code double}.
* @see java.lang.Double#valueOf(String)
* @since 1.2
*/
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}