So, I've messed with this program the last few days and I still am having trouble with it. The problem is it keeps crashing. The program is supposed to ask the user to input a decimal number. If the input is a string or a negative number, then it should spit out some output asking the user to input a decimal number again and again until they do.
import java.util.Scanner; public class decimalNumber { public static void main(String[] args) { Scanner Input = new Scanner(System.in); Double dec; double x = 0; boolean valid = true; System.out.println("Enter a decimal number: "); do { try { dec = Input.nextDouble(); valid = true; } catch (java.util.InputMismatchException e) { System.out.println("Invalid inches value. Must be a decimal number."); dec = Input.nextDouble(); } if (dec <= 0) { System.out.println("Please enter a positive number."); dec = Input.nextDouble(); } else { System.out.println(dec); } }while(!valid); System.out.println("You entered " + dec + "."); } }