Welcome to Dream.In.Code
Become a Java Expert!

Join 150,369 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,814 people online right now. Registration is fast and FREE... Join Now!




Help with exception problem

 
Reply to this topicStart new topic

Help with exception problem

rustler
18 Feb, 2008 - 10:07 PM
Post #1

New D.I.C Head
*

Joined: 10 Feb, 2008
Posts: 9

Hey i am writing some code for a homework problem and am currently writing some exceptions so the program well not crash, but i have ran into a problem.
CODE

case 1:
                     temp = JOptionPane.showInputDialog("Input a flight number that the person is going to:");  
                     temp1 = JOptionPane.showInputDialog("Input the person's name to be added to the queue:");
                
                 pq.addFlight(Integer.parseInt(temp), temp1);
                 break;

I think this is all the code needed, i declared all my variables earlier and it runs just fine now. However, I want to put a try/catch statement after temp is entered to make sure it is an integer. When i tried to do it, down below at my call pq.addFlight it said temp had not been declared.
Here's how i did it
CODE

case 1:
                     try{
                     temp = JOptionPane.showInputDialog("Input a flight number that the person is going to:");  
                     }
                     catch(Exception e){
                         JOptionPane.showMessageDialog(null, "Error: You must enter an integer for the flight number.");
                     }
                     temp1 = JOptionPane.showInputDialog("Input the person's name to be added to the queue:");
                
                 pq.addFlight(Integer.parseInt(temp), temp1);
                 break;

Is there a why to get around this? Thanks

User is offlineProfile CardPM
+Quote Post

Nayana
RE: Help With Exception Problem
18 Feb, 2008 - 10:17 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Just put temp = 0; before the try/catch statement.

This post has been edited by Nayana: 18 Feb, 2008 - 10:17 PM
User is offlineProfile CardPM
+Quote Post

rustler
RE: Help With Exception Problem
18 Feb, 2008 - 10:25 PM
Post #3

New D.I.C Head
*

Joined: 10 Feb, 2008
Posts: 9

ok thanks for the help
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Help With Exception Problem
18 Feb, 2008 - 10:30 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
The reason the problem occurred is because you didn't assign a value until the function was inside the try/catch.

The compiler knows that whatever happens inside a try may not actually happen, so it just assumes that it doesn't. Therefore, variables that are going to be used after the try...catch should be assigned values beforehand.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month