2015-03-08 57 views
-1

我正在製作一個程序,用來計算用戶輸入字符串中字母的頻率,並且最近遇到了「算術異常」錯誤。Java中的算術異常,如何處理它?

我不能爲我的生活找不出什麼原因引起的,儘管我知道這是因爲事情是由0

這裏分是我的代碼:

package day1.examples; 

import java.util.Scanner; 

public class rl_frequency_count { 

    public static int input; 

    public static void main(String[] args) { 

     System.out 
       .println("Please enter some text that you would like to work out the occurence for."); 
     System.out 
       .println("However, do remember that any other characters outside of the alphabet will NOT be counted."); 

     Scanner stringUser = new Scanner(System.in); 
     String input = stringUser.nextLine(); 
     input = input.replaceAll("\\s+", ""); 
     input = input.toLowerCase(); 

     // counting occurrence of character with loop 
     int i; 
     int charCountA = 0; 
     int charCountB = 0; 
     int charCountC = 0; 
     int charCountD = 0; 
     int charCountE = 0; 
     int charCountF = 0; 
     int charCountG = 0; 
     int charCountH = 0; 
     int charCountI = 0; 
     int charCountJ = 0; 
     int charCountK = 0; 
     int charCountL = 0; 
     int charCountM = 0; 
     int charCountN = 0; 
     int charCountO = 0; 

     for (i = 0; i < input.length(); i++) { 
      if (input.charAt(i) == 'a') { 
       charCountA++; 
       getOccurence(charCountA, "A"); 
      } 
     } 
     for (i = 0; i < input.length(); i++) { 
      if (input.charAt(i) == 'b') { 
       charCountB++; 
       getOccurence(charCountB, "B"); 
      } 
     } 
     for (i = 0; i < input.length(); i++) { 
      if (input.charAt(i) == 'c') { 
       charCountC++; 
       getOccurence(charCountC, "C"); 
      } 
     } 
     for (i = 0; i < input.length(); i++) { 
      if (input.charAt(i) == 'm') { 
       charCountM++; 
       getOccurence(charCountM, "M"); 
      } 
     } 
    } 

    // method for the occurrence 
    public static void getOccurence(int number, String letter) { 
     double occ = number/input * 10; // 
     System.out.println(); 
     System.out.println("Number of " + letter + "'s - " + number); 
     System.out.println("Occurence of " + letter + " - " + occ + "%"); 
    } 
} 

我知道,我只目前有ABC和M,但將在稍後工作。

這是我第一次發佈在這裏,我仍然對Java很陌生,所以任何幫助非常感謝!

+2

你有兩個變量命名'input':一個是在你的'main'方法中有'String',另一個是'int',它是你的類中的一個字段。你永遠不會把值賦給第二個值,所以它總是等於零。因此,'double occ = number/input * 10;'總會產生除零除外。 – yole 2015-03-08 20:57:00

回答

1

我跑了它,它說67行這裏是總:

public static void getOccurence(int number,String letter){ 
    double occ = number/input *10; // 
    System.out.println(); 
    System.out.println("Number of "+ letter +"'s - "+ number); 
    System.out.println("Occurence of "+ letter +" - "+ occ + "%"); 
} 

要解決:

double occ = (number > 0) ? number/input * 10 : 0; 

這臺OCC爲0號的情況下被設定爲0好運氣。 希望這有助於。

1

的代碼導致錯誤的線是在你的方法:

public static void getOccurence(int number,String letter){ 
    double occ = number/input *10; // <------ERROR FROM HERE (input is always 0) 
    System.out.println(); 
    System.out.println("Number of "+ letter +"'s - "+ number); 
    System.out.println("Occurence of "+ letter +" - "+ occ + "%");  
    } 

input變量在你的類這裏聲明:

Line 6: public static int input; 

既然你沒有初始化它也沒有了值在您的代碼中被更改,input的值在整個程序中保持爲0。 (對於未初始化int變量默認值爲0)

因爲它始終是0,你總是將一些以0

double occ = number/0*10;