Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

one error in Java code Please help?

Hi everyone

I am writing this code that uses an input box that has three choices and I am having trouble parsing the value afterward :

import java.io.*;

import javax.swing.JOptionPane;

public class MyType

{

public static void main (String[] args)

{

// Declare and Construct Variables

String strChoice, strTryString, strTryInt, strTryDouble;

int choice, tryInt;

double tryDouble;

boolean done = false;

//loop while user does not click cancel button

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);

switch(choice)

{

case 1:

JOptionPane.showMessageDialog(null, "Correct, any input can be saved as 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:

JOptionPane.showMessageDialog(null, "Exit.");

done = true;

break;

default:

throw new NumberFormatException();

}

}

catch(NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Please try again");

}

}

}

}

Thank You :-)

Update:

This is the error: C:\Users\user\Desktop\MyType.java:31: variable strChoice might not have been initialized

choice = Integer.parseInt(strChoice);

^

1 error

Thank You :-)

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    You haven't assigned the user input value to strChoice.

Still have questions? Get your answers by asking now.