Java算术异常,如何处理?

Arithmetic Exception in Java, how to deal with it?

我正在制作一个计算用户输入字符串中字母出现频率的程序,最近遇到了 'Arithmetic Exception' 错误。

我这辈子都弄不清楚是什么原因造成的,尽管我知道这是因为某些东西被除以 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 还是个新手,所以非常感谢任何帮助!

我 运行 它显示第 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;

如果 number 设置为 0,这会将 occ 设置为 0。祝你好运。 希望这有帮助。

导致错误的代码行在您的方法中:

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 变量在您的 class 此处声明:

Line 6: public static int input;

因为您没有初始化它,也没有在您的代码中更改值,所以 input 的值在整个程序中保持为 0。 (未初始化的 int 变量的默认值为 0)

因为它总是0,所以你总是用0除一个数。

double occ = number / 0*10;