简体   繁体   English

Java中的算术异常,如何处理?

[英]Arithmetic Exception in Java, how to deal with it?

I'm making a program that counts the frequency of letters from a user-entered string, and have recently encountered the 'Arithmetic Exception' error. 我正在编写一个程序,该程序计算用户输入的字符串中字母的出现频率,并且最近遇到了“算术异常”错误。

I cannot for the life of me figure out what's causing it, even though I know it's because something is being divided by 0. 即使我知道这是因为某物被零除,我也无法弄清楚是什么原因。

Here's my code: 这是我的代码:

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 + "%");
    }
}

I know that I only have ABC and M in at the moment but was gonna work those in later. 我知道目前我只有ABC和M,但稍后会继续工作。

This is the first time i've posted on here and i'm still newish to Java so any help whatsoever is greatly appreciated! 这是我第一次在这里发布文章,但我仍然对Java还是陌生的,因此,任何帮助都将不胜感激!

I ran it and it says line 67. here is the total: 我运行它,并说第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 + "%");
}

To fix: 修理:

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

This sets occ to 0 in case of number being set to 0. Good luck. 如果数字设置为0,则将occ设置为0。祝您好运。 Hope this helps. 希望这可以帮助。

The line of code causing the error is in your method: 导致错误的代码行在您的方法中:

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 + "%");       
  }

The input variable is declared in your class here: input变量在您的类中在这里声明:

Line 6: public static int input;

Since you didn't initialize it nor does the value is being changed in your codes, the value of input remains as 0 through out the entire program. 由于您没有初始化它,也没有在代码中更改值,因此在整个程序中, input的值始终为0。 (Default value for an uninitialized int variable is 0) (未初始化的int变量的默认值为0)

Since it is always 0, you are always dividing a number with 0. 由于它始终为0,因此您总是将数字除以0。

double occ = number / 0*10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM