Question
What is wrong with my code? I keep compiling it (Java) and I keep getting this - whileloop.java:19: error: 'catch' without 'try' catch (java.util.InputMismatchException /*java.lang.NumberFormatException*/
What is wrong with my code? I keep compiling it (Java) and I keep getting this -
whileloop.java:19: error: 'catch' without 'try'
catch (java.util.InputMismatchException /*java.lang.NumberFormatException*/ e) {
^
whileloop.java:9: error: 'try' without 'catch', 'finally' or resource declarations
try {
^
2 errors
import java.util.Scanner;
class whileloop {
public static void main(String[] args) {
double input = 0;
Scanner in = new Scanner(System.in);
while (true) {
try {
System.out.print("Enter a number or type quit to exit: ");
String stringinput = in.next();
if (stringinput.equals("quit")) {
//System.exit(1);
System.out.print("Program exiting ");
break;
}
input = Double.parseDouble(stringinput);
//input = in.nextDouble();
catch (java.util.InputMismatchException /*java.lang.NumberFormatException*/ e) {
// The first argument isn't a valid integer. Print
// an error message, then exit with an error code.
System.out.println("The argument must be a double. ");
//System.out.println(" ");
//System.exit(0);
//return;
}
if (input < 0) {
System.out.println("The number can't be negative");
System.out.println(" ");
}
else {
double sqmiles = sqtosq(input);
System.out.println(input + " is the input and " + sqmiles + " square mile(s)");
System.out.println(" ");
}
}
}
}
public static double sqtosq(double kilometers) {
double miles = kilometers / 2.59;
double sqmiles = miles * miles;
return sqmiles;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started