2012-05-31 24 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; 

這應該做的伎倆