2012-05-31 28 views
2

我想捕获插入文本并且程序必须parseInt的异常。 编译时它说变量num可能没有被初始化。 (第31行) 我不知道为什么它会这样做。Java异常处理变量未初始化

代码如下。提前致谢。

// Java packages 
import javax.swing.JOptionPane; 
// program uses JOptionPane 

class Help2Gui { 
    public static void main(String args[]) { 

     // variable declaration 
     String choice; 
     int num; 

     // read in first number from user as a string 
     choice = 
      JOptionPane 
       .showInputDialog("Help on: \n \t 1. class \n \t 2. object \n \t 3. method \n \t 4. variable \n \t 5. constructor \n \t 6. Quit \n Enter a number from the list above."); 

     if (choice == null) { 
      System.exit(0); 
     } 

     do { // begin a loop to display initial choice, repeating until 6 is 
      // entered 

      // convert numbers from type String to type int 
      try { 
       num = Integer.parseInt(choice); 
      } 

      catch (NumberFormatException nfe) { 

      } 

      switch (num) { // display result for each item entered by user 
      case 1: 
       JOptionPane.showMessageDialog(null, 
        "\n A class is a definition of an object.", 
        "Java Help System", JOptionPane.PLAIN_MESSAGE); 
       break; 

      case 2: 
       JOptionPane 
        .showMessageDialog(
         null, 
         "The switch: \n \n switch (expression) { \n case constant: \n statement sequence \n break \n // ... \n } ; ", 
         "Java Help System", JOptionPane.QUESTION_MESSAGE); 
       break; 

      case 3: 
       JOptionPane 
        .showMessageDialog(
         null, 
         "The for: \n \n for(init; condition; iteration) \n statement;", 
         "Java Help System", JOptionPane.INFORMATION_MESSAGE); 
       break; 

      case 4: 
       JOptionPane.showMessageDialog(null, 
        "The while: \n \n while(condition) statement;", 
        "Java Help System", JOptionPane.WARNING_MESSAGE); 
       break; 

      case 5: 
       JOptionPane 
        .showMessageDialog(
         null, 
         "The do-while: \n \n do { \n statement; \n } while (condition);", 
         "Java Help System", JOptionPane.ERROR_MESSAGE); 
       break; 

      case 6: 
       System.exit(0); 
       break; 

      default: 
       JOptionPane.showMessageDialog(null, 
        "Enter a number from 1 to 5 or 6 to Quit", 
        "Java Help System", JOptionPane.ERROR_MESSAGE); 
      } 

      // read in first number from user as a string 
      choice = 
       JOptionPane 
        .showInputDialog("Help on: \n \t 1. if \n \t 2. switch \n \t 3. for \n \t 4. while \n \t 5. do-while \n \t 6. Quit \n Enter a number from the list above."); 
      try { 
       // attempt to convert the String to an int 
       num = Integer.parseInt(choice); 

      } 
      catch (NumberFormatException nfe) { 
       JOptionPane.showMessageDialog(null, 
        "Enter a number from 1 to 5 or 6 to Quit", 
        "Java Help System", JOptionPane.ERROR_MESSAGE); 
      } 
      // convert numbers from type String to type int 

     } 
     while (num != 6); // end of do-while loop. 

     System.exit(0); // terminate application with window 

    } // end method main 

} // end class Help2Gui 
+0

哇你快。感谢大家。问题解决了。 – user1428565

回答

4

如果抛出异常,并且抓住了,变量num不受

num = Integer.parseInt(choice); 

初始化,然后会出现一个问题:

switch (num) { //what is num in here? 

为了解决这个问题 - 你可以为这些情况给出默认值(在异常处理程序中,或在try块之前,因此num的分配将覆盖默认值)或终止在引发异常时使用该方法。
简单的解决方案恕我直言(尽管它不总是符合)将初始化num同时宣布它,像:

int num = MY_DEFAULT_VALUE; 
0

如果parseInt抛出一个异常,NUM未初始化。将其初始化为catch块之前的内容。

0

问题是你这样做:int num;。你没有给它分配任何价值。在你的do while循环中你正在填充这个值,但是这可能会抛出一个异常,这会导致你的变量在到达switch语句时仍然未被初始化。我建议你用一些初始的回退值填充你的变量num,然后更新循环内的值。

作为另一个建议,我会建议您不要吞下异常,并且在捕获部分内,如果发现异常,您将为num变量提供一些值。

+0

“作为另一项建议,我建议您不要吞下异常,并且在catch部分中为num变量提供一些值,以便发现异常。” 这帮助我没有尽头。 – user1428565

0

num=0;在catch为隐含的默认

0

初始化try块前num。如果从类型转换中抛出任何异常,那么num将不会被初始化。如果可能的话,对于一个好的编程练习,在使用它之前初始化所有的变量。

0

这是因为如果抛出异常,你的变量num没有用任何值初始化。在这种情况下,所有操作都会失败,因为num不会有值。如果num是方法范围之外的变量,那么它将得到一个默认值。但在方法内声明的变量将不会收到任何默认值。您可以在引发异常时为num分配一个单独的值,或者在声明异常时初始化它。基本上你有很多关于如何处理这个问题的选项。

0

替代由

int num = 0; 

这应该做的伎俩