Trying to write a program in which the user inputs their expenses for the week. The trouble comes in the fact that I want the program to re-ask the user if unacceptable input is entered. I figured out how to detect negative values, but when trying to catch an Input Mismatch Exception (if like a character or string is inputted) the loop just runs infinitely asking for "Monday Expenses:" How do I make it so the user is given another chance to answer? I tried a break; but that broke out of the do while loop also, which I don't want.
Here's my code so far:
import java.util.Scanner;
public class BarGraph{
public static void main (String[] args){
Scanner myScanner;
myScanner = new Scanner(System.in);
double mon, tues, wed, thurs, fri, sat, sun;
do{
try{
System.out.print("Expenses for Monday: ");
mon = myScanner.nextDouble();
}catch( Exception e){
mon = -1;
}
}while(mon<0);
System.out.println(mon);
}
}
Thanks for your help