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

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




switch and try statements

 
Reply to this topicStart new topic

switch and try statements, finding out what causes my errors

monkeymansblues
4 Feb, 2008 - 04:54 PM
Post #1

New D.I.C Head
*

Joined: 4 Feb, 2008
Posts: 3

Hi I have some problems with an error msg saying
ava:37: variable choice might not have been initialized
switch(choice)

I was hoping that someone could tell me the error I mean whats really causing it. I saw that someone else had the same problem but it didnt help and I was just hoping someone might have a some advice for me.

CODE

import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
    public static void main(String[] args)
    {
        //declaring variables
        String strChoice = " ", strTryString, strTryInt, strTryDouble;
        int choice, tryInt;
        double tryDouble;
        boolean done = false;
        double A;


        //loop while not done
        while (!done)
        {

            try
                {
                    String message = "What is My Type?:" + "\n\n1)String\n2)Integer\n3)double\n4)Quit the program\n\n";
                    
                    choice = Integer.parseInt(strChoice);

                    //test for valid choice 1, 2, 3, or 4
                    if (choice<1 || choice>4) throw new NumberFormatException();
                    else done = true;

                }

            catch (NumberFormatException e)

                {
                    JOptionPane.showMessageDialog(null, "Please enter a 1, 2, 3 or 4:",  "Error", JOptionPane.INFORMATION_MESSAGE);

                        switch(choice)

                            {
                                case 1:

                                    JOptionPane.showInputDialog(null,"Any Letter Is A String");
                                    break;
                                 case 2:
                                    JOptionPane.showMessageDialog(null, "Correct!");
                                    tryInt = Integer.parseInt(strChoice);
                                    break;


                                case 3:
                                     JOptionPane.showMessageDialog(null, "Correct!");
                                        tryDouble = Integer.parseInt(strChoice);
                                        break;
                                case 4:
                                    done = true; JOptionPane.showMessageDialog(null,"Closing!");
                                    System.exit(0);
                                    break;
                                    default:  throw new NumberFormatException();
                            }

                    }

                            {

                            JOptionPane.showMessageDialog(null, "Invalid, try again.");
                            }



        }
    }
}
~edit: code tags added PB

This post has been edited by PennyBoki: 4 Feb, 2008 - 05:30 PM
User is offlineProfile CardPM
+Quote Post

thenovices
RE: Switch And Try Statements
4 Feb, 2008 - 05:22 PM
Post #2

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
first of all, always put "[code]" and "[/ code]" (without the space and the quotations) around your code so its more readable.

And, you should probably just try initializing your choice to 0 when declaring it.

This post has been edited by thenovices: 4 Feb, 2008 - 05:25 PM
User is offlineProfile CardPM
+Quote Post

monkeymansblues
RE: Switch And Try Statements
4 Feb, 2008 - 05:45 PM
Post #3

New D.I.C Head
*

Joined: 4 Feb, 2008
Posts: 3

QUOTE(thenovices @ 4 Feb, 2008 - 06:22 PM) *

first of all, always put "[code]" and "[/ code]" (without the space and the quotations) around your code so its more readable.

And, you should probably just try initializing your choice to 0 when declaring it.



Hey thanks for the input about initializing my choice. What did you mean by always put "[code]" and "[/ code]" (without the space and the quotations) around your code so its more readable? I wanna make sure I dont do the same mistake again.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Switch And Try Statements
4 Feb, 2008 - 05:48 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
when posting code code.gif
User is offlineProfile CardPM
+Quote Post

monkeymansblues
RE: Switch And Try Statements
4 Feb, 2008 - 05:49 PM
Post #5

New D.I.C Head
*

Joined: 4 Feb, 2008
Posts: 3

QUOTE(PennyBoki @ 4 Feb, 2008 - 06:48 PM) *

when posting code code.gif


thanks will do!
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Switch And Try Statements
4 Feb, 2008 - 06:53 PM
Post #6

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
set choice = 0; on top when you declare it. that will compile but i think you still have logic errors.
User is offlineProfile CardPM
+Quote Post

fsloke
RE: Switch And Try Statements
4 Feb, 2008 - 08:28 PM
Post #7

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Java:37: variable choice might not have been initialized

this error mean you maybe declare choice inside the loop...

pls initialized before the loop. You can put it as zero "0".

then you switch there have to change something.... better don't put thrown exception on the default there....

put default as
"you have enter wrong choice.... pls select 1-4 option that listed"

hope can help
User is offlineProfile CardPM
+Quote Post

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

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