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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!