2012-05-12 21 views
0

我改變了我的代碼爲丹建議我現在可以編譯程序,但是,無論輸入是什麼,結果總是2.我把第二部分這個程序在新代碼下面。請幫忙。除了線程「主」java.lang.NullPointerException -part2

以下是新代碼。

public class VowelCons 
     { 
    private final String str; 
    private final int totalConsonants; 
    private final int totalVowels; 

     public VowelCons(final String s) 
    { 
      this.str = s; 
       int totalConsonants = 0; 
       int totalVowels = 0; 
       if (null != s) 
     { 
        for (final char c : s.toCharArray()) 
      { 
           switch (c) 
        { 
             case 'A': 
             case 'a': 
             case 'E': 
             case 'e': 
             case 'I': 
             case 'i': 
             case 'O': 
             case 'o': 
             case 'U': 
             case 'u': 

         totalVowels++; 
              break; 

         default: 

         if (Character.isLetter(c)) 
         { 
                totalConsonants++; 
              } 
              break; 
            } 
         } 
       } 
      this.totalConsonants = totalConsonants; 
      this.totalVowels = totalVowels; 
     } 


    public String getString() 
    { 
      return str; 
    } 

    public int getNumConsonants() 
    { 
       return this.totalConsonants; 
    } 

    public int getNumVowels() 
    { 
       return this.totalConsonants; 
    } 
} 

有這個計劃,得到了用戶的輸入,並把它傳遞給這一類的其他部分。 這是代碼。

import java.util.Scanner; 

    public class VowelConsCounter 
    { 
     public static void main(String[] args) 
     { 
     String input;  // User input 
     char selection;  // Menu selection 

     Scanner keyboard = new Scanner(System.in); 

     System.out.print("Enter a string: "); 
     input = keyboard.nextLine(); 

     VowelCons vc = new VowelCons(input); 

     do 
     { 
      selection = getMenuSelection(); 

      switch(Character.toLowerCase(selection)) 
      { 
      case 'a' : System.out.println("\nNumber of vowels: " + 
         vc.getNumVowels()); 
         break; 
      case 'b' : System.out.println("\nNumber of consonants: " + 
         vc.getNumConsonants()); 
         break; 
      case 'c' : System.out.println("\nNumber of vowels: " + 
         vc.getNumVowels()); 
         System.out.println("Number of consonants: " + 
         vc.getNumConsonants()); 
         break; 
      case 'd' : System.out.print("Enter a string: "); 
         input = keyboard.nextLine(); 
         vc = new VowelCons(input); 
     } 

     } while (Character.toLowerCase(selection) != 'e'); 

    } 

    public static char getMenuSelection() 
    { 
     String input;  
     char selection; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.println("a) Count the number of vowels in the string."); 
     System.out.println("b) Count the number of consonants in the string."); 
     System.out.println("c) Count both the vowels and consonants in the string."); 
     System.out.println("d) Enter another string."); 
     System.out.println("e) Exit the program."); 

     input = keyboard.nextLine(); 
     selection = input.charAt(0); 

     while (Character.toLowerCase(selection) < 'a' || Character.toLowerCase(selection) > 'e') 
     { 
     System.out.print("Only enter a, b, c, d, or e: "); 
     input = keyboard.nextLine(); 
     selection = input.charAt(0); 
     } 

     return selection; 
    } 
} 

回答

0

你得到一個NullPointerException,因爲你沒有初始化實例變量result

我會建議使用下列內容:

public class VowelCons { 
    private final String str; 
    private final int totalConsonants; 
    private final int totalVowels; 

    public VowelCons(final String s) { 
     this.str = s; 
     int totalConsonants = 0; 
     int totalVowels = 0; 
     if (null != s) { 
      for (final char c : s.toCharArray()) { 
       switch (c) { 
        case 'A': 
        case 'a': 
        case 'E': 
        case 'e': 
        case 'I': 
        case 'i': 
        case 'O': 
        case 'o': 
        case 'U': 
        case 'u': 
         totalVowels++; 
         break; 
        default: 
         if (Character.isAlphabetic(c)) { 
          totalConsonants++; 
         } 
         break; 
       } 
      } 
     } 
     this.totalConsonants = totalConsonants; 
     this.totalVowels = totalVowels; 
    } 

    public String getString() { 
     return str; 
    } 

    public int getTotalConsonants() { 
     return this.totalConsonants; 
    } 

    public int getTotalVowels() { 
     return this.totalConsonants; 
    } 

    public String toString() { 
     return (null == str ? "" : str) + " [consonants=" + totalConsonants + ", vowels=" + totalVowels + "]"; 
    } 

    public static void main(final String[] args) { 
     for (final String arg : args) { 
      final VowelCons vc = new VowelCons(arg); 
      System.out.println(vc.toString()); 
     } 
    } 
} 

這,例如,輸出:

$ java VowelCons foo BaR "Lady GODIVA" 
foo [consonants=1, vowels=2] 
BaR [consonants=2, vowels=1] 
Lady GODIVA [consonants=6, vowels=4] 

這裏有幾點這個例子應該幫助你學習:

  1. 局部變量可能隱藏實例變量(請參見[1][2])。
  2. 使用this來引用實例變量(請參閱[1])。您應該始終使用this引用實例變量,這樣可以防止在將來的代碼更改時意外隱藏它,並允許IDE提供僅包含實例成員的上下文相關建議。
  3. 把手nullString s傳遞給構造函數。
  4. 使用switch可以簡化邏輯並減少if-else邏輯中的冗餘代碼。
  5. 檢查小寫和大寫元音。
  6. 忽略元音/輔音計數中的非字母字符。
  7. 實施自定義toString()
+0

謝謝大師。我改變了很多我的代碼。因爲這個程序的另一部分將把用戶字符串傳遞給這個類,所以我不能改變很多返回結果。無論如何,我用Dan的建議來重寫VowelCons類。但無論我輸入什麼,結果是2.想知道爲什麼,謝謝大師。 – LPlateJava

+0

對不起隊友,我的錯誤。只是因爲錯誤的方案沒有奏效。但現在我修復它。它完美地工作。非常感謝。 – LPlateJava

3

它看起來像你初始化本地數組result,但隨後試圖從成員陣列result閱讀[這部分不能按規定改變。由於您尚未初始化成員,因此仍然是null,因此您看到的是java.lang.NullPointerException

您可能想要將countVowelsAndCons更改爲void返回類型,並且擺脫本地result。然後,您需要確保在撥打getNumVowelsgetNumConsonants之前致電。順便說一句,像index這樣的東西應該是您的成員函數中的局部變量 - 它們不屬於類範圍。

但更重要的是,這可能不應該是一個類。你可能想是這樣的:

private static boolean isVowel(char c) 
{ 
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; 
} 

public static int countConsonants(String s) 
{ 
    int count = 0; 
    for(int i=0, len=s.length(); i<len; ++i) 
    { 
     if(!isVowel(s.charAt(i))) ++count; 
    } 
    return count; 
} 

public static int countVowels(String s) 
{ 
    int count = 0; 
    for(int i=0, len=s.length(); i<len; ++i) 
    { 
     if(isVowel(s.charAt(i))) ++count; 
    } 
    return count; 
} 
相關問題