Hi I'm a beginner in Java and I am trying to write this program where I can enter numbers, but that when I enter "done", I get the values that I called total, numberInputs. However when i run it and that i input "done" I get Exception in thread "main" java.lang.NumberFormatException: For input string: "done". Do you know how I can fix this please?
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at repeat.numberReader(repeat.java:15)
at repeat.main(repeat.java:23)
This is my code:
import java.util.Scanner;
public class repeat{
public void numberReader(){
Scanner myScanner = new Scanner(System.in);
int total = 1;
int numberInputs = 1;
String userInput;
do {
System.out.println("Enter a number: ");
userInput = myScanner.nextLine();
total = total + Integer.parseInt(userInput);
numberInputs++;
} while (!"done".equals(userInput));
System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
}
public static void main(String[] args){
repeat instance = new repeat();
instance.numberReader();
}
}
Thank you for your help