3 Attachment(s)
try and catch not working with double values??
I am trying to add a try and catch setup to catch and respond to exceptions in use input, but every time I add try( to my code, it comes back complaining to change my double variables to int, but if I do that it messes up the rest of the program.... :smash: This is my code:
Code:
package annual;
import java.util.*;
import java.lang.*;
public class Annual {
public static void main(String[] args)
{ double T = 50000;
double Amt;
Scanner percent = new Scanner(System.in);
System.out.println("Please enter annual sales");
System.out.println("please enter a numeric value to avoid program crash:");
double yearly = percent.nextDouble();
if (yearly >= 0 && yearly <= 20000000){
double YrPrcnt = yearly * .05;
Amt = T + YrPrcnt;
System.out.print("The Annual Payrate for employee is:");
System.out.print(Amt);}
else {
System.err.println("Sales too high");
System.err.print("Please see Human Resources.");
}
}}
and this works, but if the user enters anything except for a numeric value, the program crashes, and I need to account for this, so I decided to use try and catch for it, but when I place a try into my code it does not like it as in this code:
Code:
package annual;
import java.util.*;
import java.lang.*;
public class Annual {
public static void main(String[] args)
{ try(
double T = 50000;
double Amt;
Scanner percent = new Scanner(System.in);
System.out.println(" Please enter annual sales");
System.out.println("please enter a numeric value to avoid program crash:");
double yearly = percent.nextDouble();
if (yearly >= 0 && yearly <= 20000000){
double YrPrcnt = yearly * .05;
Amt = T + YrPrcnt;
System.out.print("The Annual Payrate for employee is:");
System.out.print(Amt);}
else {
System.err.println("Sales too high");
System.err.print("Please see Human Resources.");
})
}}
What is happening here is that it is instructing me to change "double T = 50000;" to an int, but if I do that it crashes the calculations and the program does not work.... :s: It also says there is a problem with the line, " System.out.println(" Please enter annual sales");" in that it cannot find symbol: class out / Location: class System, but I thought that System.out.println() was an inherent method within the Java language?? :(doh):
Since I cannot seem to add attachments to my threads here due to some technical hiccup the site is having, I will place my screenshots in the body of my question:
Attachment 4239Attachment 4240Attachment 4241
Anyway, if some can please tell me what is wrong with this, so that I can finish up here, I will be in your debt!!
Re: try and catch not working with double values??
Your try-catch should be like this:
Code:
try {
// the code that might thrown exception goes here.
} catch (Exception e) {
// handles the exception here
}
In your code above it seems that you are using try-catch with resource statement. You use this kind of try-catch if you want it to handle resource management automatically such as closing a JDBC connection or closing a stream.