Results 1 to 2 of 2
- 11-19-2012, 12:48 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
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:
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) { 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....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."); }) }}
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:



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!!
- 11-19-2012, 04:56 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: try and catch not working with double values??
Your try-catch should be like this:
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.Java Code:try { // the code that might thrown exception goes here. } catch (Exception e) { // handles the exception here }Last edited by wsaryada; 11-19-2012 at 07:05 AM.
Website: Learn Java by Examples
Similar Threads
-
find the unique values of a double array
By tyang in forum New To JavaReplies: 3Last Post: 09-11-2011, 02:47 PM -
Substring double values?
By JayP in forum New To JavaReplies: 5Last Post: 06-06-2011, 03:54 AM -
Double with no negative values.
By vahini in forum New To JavaReplies: 11Last Post: 05-05-2011, 09:07 AM -
Removing the double values?
By Lund01 in forum Advanced JavaReplies: 13Last Post: 11-17-2010, 11:34 AM -
mutliplicatio of double values
By katkamravi in forum New To JavaReplies: 2Last Post: 04-13-2009, 02:28 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks