#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    2
    Rep Power
    0

    Compile Error (variable might not have been initialized)


    thank your for your help.

    the program uses switch statments to to validate user input.

    the programe basicly is used to test wether or not what is in the text box is a string a integer or a double.(i dont make up the assinments i just do them.)

    the errors are:
    30: variable strChoice might not have been initialized
    choice = Integer.parseInt(strChoice);
    ^
    40: variable strTryInt might not have been initialized
    Integer.parseInt(strTryInt);
    ^
    46: variable strTryDouble might not have been initialized
    Double.parseDouble(strTryDouble);
    ^
    the Source code is:
    sorry about posting the whole thing i just dont know were the problem is.
    i think it is a simple error but i dont really know anyone who knows java. thank you for your help

    Code:
     
    
    import java.io.*;
    import javax.swing.JOptionPane;
    
    
    	public class MyType
    	{
    		public static void main (String[] args) throws IOException
    		{
    
    			String strChoice, strTryString, strTryInt, strTryDouble;
    			int choice, tryInt;
    			double tryDouble;
    			boolean done = false;
    
    			while(!done)
    			{
    				String answer = JOptionPane.showInputDialog(null, "Enter a choice or click Cancel to exit:");
    				if (answer == null) finish();
    
    				try
    				{
    					String message = ("What's my Type?" + "/n/n1) string/n2) integer/n3) double/n4) Quit the program/n/n");
    					choice = Integer.parseInt(strChoice);
    
    					switch(choice)
    						{
    						case 1:
    							System.out.println("Enter a string .");
    							System.out.println("Correct becuse any anwser can be stored as a string");
    							break;
    						case 2:
    							System.out.println("Enter an Integer");
    							Integer.parseInt(strTryInt);
    							System.out.println("Correct");
    							break;
    
    						case 3:
    							System.out.println("!Enter a double");
    							Double.parseDouble(strTryDouble);
    							System.out.println("Correct");
    							break;
    						case 4:
    							System.out.println("Enter anything to Finish");
    							done = true;
    							System.out.println("You are finished");
    							break;
    						default:
    							throw new NumberFormatException();
    						}
    					}
    				catch (NumberFormatException e)
    				{
    					System.out.println("Your entry was incorrect, try again.");
    				}
    
    			}
    
    		}
    
    	public static void finish()
    	{
    		System.exit(0);
    	}
    }
  2. #2
  3. Gert Cuppens
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Antwerpen - Belgium
    Posts
    484
    Rep Power
    27
    the solution is in the error message
    30: variable strChoice might not have been initialized
    choice = Integer.parseInt(strChoice);

    Initialise strChoice and you won't have a problem.
    String strChoice = "";
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    2
    Rep Power
    0

    thanks


    thanks for the help not quiet what i was llokin for but it helped
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Unhappy


    Originally Posted by gertcuppens
    the solution is in the error message
    30: variable strChoice might not have been initialized
    choice = Integer.parseInt(strChoice);

    Initialise strChoice and you won't have a problem.
    String strChoice = "";

    I'm working on exact same problem for school. Mine is 100% for sure initialized. (Step 6 gives exact code)

    Ay other thoughts?

    Thanks guys.
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by andianxiety
    I'm working on exact same problem for school. Mine is 100% for sure initialized. (Step 6 gives exact code)

    Ay other thoughts?

    Thanks guys.
    can you post your code?

IMN logo majestic logo threadwatch logo seochat tools logo