Ok, I'm done the program now. Only thing left that I want to do is flash up a window on my exceptions stating the error. So lets say someone enters a negative number for their payment in the payment field and they hit enter I want a window to flash up saying "Invalid payment amount. Must be greater than zero." Any idea on how to do this? Thanks.
Announcement
Collapse
No announcement yet.
Programming question
Collapse
X
-
Boring. Give them an interesting message like: "WTF! You can't make a negative payment. ****."“As a lifelong member of the Columbia Business School community, I adhere to the principles of truth, integrity, and respect. I will not lie, cheat, steal, or tolerate those who do.”
"Capitalism ho!"
Comment
-
Originally posted by DriXnaK View PostOk, I'm done the program now. Only thing left that I want to do is flash up a window on my exceptions stating the error. So lets say someone enters a negative number for their payment in the payment field and they hit enter I want a window to flash up saying "Invalid payment amount. Must be greater than zero." Any idea on how to do this? Thanks.
Step 2:
Code:try { //code that could throw an exception goes here } catch (AppropriateExceptionType e) { //code to handle exception, should it occur, goes here }
Comment
-
Ok, I'm lost. Spent probably an hour now maybe trying to figure this out. Here's my latest attempt.
try {
Double.parseDouble(payment.getText());
}
catch (IllegalArgumentException exception){
if (Double.parseDouble(payment.getText()) < 0);
System.out.println("Invalid payment amount.");
}
This one down below is my original exception that worked but didn't tell the user that there had been an invalid payment amount entered:
if (paymentValue < 0)
{
IllegalArgumentException exception
= new IllegalArgumentException("Invalid payment amount.");
throw exception;
}Last edited by DriXnaK; October 20, 2010, 16:44.
Comment
-
Code:parseDouble public static double parseDouble(String s) throws NumberFormatException Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double. Parameters: s - the string to be parsed. Returns: the double value represented by the string argument. Throws: NumberFormatException - if the string does not contain a parsable double. Since: 1.2 See Also: valueOf(String)
Comment
-
I tried changing the exception to the proper one but it's not doing anything still. I'm thinking my try statement is incorrect. Here's what I have:
try {
if (Double.parseDouble(payment.getText()) < 0);
}
catch (NumberFormatException exception){
System.out.println("Invalid payment amount.");
}
I've read the book's explanation of it probably 10 times now but I'm not getting it. Here's the example from the book. I'm having trouble trying to relate to this example and apply it to what I'm trying to do. I'm pretty sure my try statement is all wrong.
try
{
String filename = . . .;
FileReader reader = new FileReader(filename);
Scanner in = new Scanner(reader);
String input = in.next();
int value = Integer.parseInt(input);
. . .
}
catch (IOException exception)
{
exception.printStackTrace();
}
catch (NumberFormatException exception)
{
System.out.println("Input was not a number");
}
Comment
-
If payment.getText() is not a valid double (e.g. "foo") then that code will execute System.out.println("Invalid payment amount.").
edit: this may be a dumb question, but you can see your Java console, right? When you call System.out.println() you can see the output.
Code:if (Double.parseDouble(payment.getText()) < 0);
Last edited by Kuciwalker; October 20, 2010, 18:19.
Comment
-
Thanks Asher. Ok and then I don't understand this statementNote: this code doesn't do anything because the text between the "if (condition)" and the next semicolon is empty.
Comment
-
Code:try { if (Double.parseDouble(payment.getText()) < 0) /* see this empty bit here? this is where the code that would be executed after the if(), if the condition were true, goes */; } catch (NumberFormatException exception) { System.out.println("Invalid payment amount."); }
Code:if ([i]condition[/i]) [i]statement[/i];
Code:if ([i]condition[/i]) { [i]a sequence of statements, each followed by a semicolon[/i] }
Comment
-
Think of the "try {" and "}" as demarking boundaries around otherwise perfectly normal code - they have nothing to do with your if statement.
If you just want to try calling Double.parseDouble() and see if it raises an exception, then all you need to do is say:
Code:try { Double.parseDouble(someString); } catch (NumberFormatException e) { //whatever code you want to execute if an exception is raised }
Comment
Comment