I am trying to add a try catch block with switches to my program in NetBeans, and it is not excepting any of my already declared variables?? Here is my original code, without any validation:
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.");
}}}
This is what I get when I try to add try and catch to this to validate the user input:
package annual;
import java.util.*;
import java.lang.*;
import javax.rmi.CORBA.Util;
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:");
try(
double yearly = percent.nextDouble();
switch(yearly)
case 1:
System.out.println("Thank you");
case 2:
System.out.println("Please enter a valid dollar value");
case 3:
System.out.println("Invalid entry, please try again");
)
catch(java.util.IllegalFormatException){
System.out.println("Please be sure you are entering a numeric value")
}
}
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.");
}}}
Giving me issues at the lines:
double yearly = percent.nextDouble(); claiming I need to set yearly as a double?? I thought that was what this line was doing though??
switch(yearly) Claiming that is expecting an int, but finding a double value??
catch(java.util.IllegalFormatException){ Claiming that it is a catch without a try?? The try was already coded in though??
System.out.println("Please be sure you are entering a numeric value") Barking at me about case, default, or '}' expected?? I have no comment for that one....
In any case, just trying to add a try and catch block to my code has completely ruined my program, and I was hoping for some wisdom to help me clear this up....
Oh yeah, and thanks in advance!!