Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Difficulty with string passing...

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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...

            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.
    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...
    Last edited by Destined; May 12th, 2011 at 05:14 AM. Reason: could be interpreted in more than one way.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  2. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  3. Passing JTextField as String parameter
    By Christophe in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2010, 05:32 AM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM

Tags for this Thread