Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By wsaryada

Thread: try and catch not working with double values??

  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question 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.... This is my code:
    Java 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:
    Java 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.... 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??
    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:
    try and catch not working with double values??-change2int.jpgtry and catch not working with double values??-doublefail.jpgtry and catch not working with double values??-system.outfail.jpg
    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!!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: try and catch not working with double values??

    Your try-catch should be like this:

    Java 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.
    Last edited by wsaryada; 11-19-2012 at 08:05 AM.
    DarrylBurke likes this.

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 03:47 PM
  2. Substring double values?
    By JayP in forum New To Java
    Replies: 5
    Last Post: 06-06-2011, 04:54 AM
  3. Double with no negative values.
    By vahini in forum New To Java
    Replies: 11
    Last Post: 05-05-2011, 10:07 AM
  4. Removing the double values?
    By Lund01 in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 12:34 PM
  5. mutliplicatio of double values
    By katkamravi in forum New To Java
    Replies: 2
    Last Post: 04-13-2009, 03:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •