Difficulty with string passing...
Hey everyone,
I am having trouble exiting a program normally, rather than it crashing whenever I click the 'cancel' button. :(
This code here is an example of what i want to do/happen...
Code :
double length = 0; // passing into
String user_input; // passing from
boolean valid = true; // to exit the loop (irrelevant)
do
{
user_input = JOptionPane.showInputDialog("Enter the length in metres: ");
try
{
length = Double.parseDouble(user_input); // code to pass string into double.
valid = true;
}
catch(NullPointerException error) // catches the error when user clicks the 'cancel' button and exits the program normally.
{
System.exit(0);
}
The code above works and doesn't crash or have syntax errors:)
I am trying to implement this with the code below, but to no avail. :(
Code :
public static void exampleMethod(String user_input)
{
String choice;
try
{
choice = String.parseString(user_input); //<--- No such thing?! WHY!?
}
catch(NullPointerException error) // catches the error when user clicks the 'cancel' button and exits the program normally.
{
System.exit(0);
}
It honestly doesn't bother me what the 'try' function does, it can do anything... or NOTHING i just want the program NOT to crash when i click the 'cancel' button. I even attempted to delete it but the compiler insists ('catch' with no 'try')
I hope this is clear enough... :)
Re: Difficulty with string passing...
Wow... I have been working on this problem for 36+ hours and I finally figured it out... It was a misunderstanding of the 'try' statement...