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!!

Member Avatar for Symbiatch

Do you really have try( ? It's a block construct, so try { ... } catch ...

Also, using doubles with switch is a no-no. If the user is typing only integers, why not read them as integers?

try and catch block always starts and ends with curly brackets like {....}
so put,and then see the output.
Also you have put extra or irregular curly braces...so check it out at line 26 or 35..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.