【问题标题】:Ascii character counter in Java error [duplicate]Java错误中的Ascii字符计数器[重复]
【发布时间】:2018-05-15 12:18:02
【问题描述】:

请忽略错误的文件名,但到目前为止我就是这样做的。我想计算Java 文件中的所有ASCII 字符,但它得到一个带有大文本的“数组越界错误”

此代码:

class CreateZipFile {


    public static void main(String[] args) {
            try {
                CharacterCounter();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println(e.getClass().getSimpleName() + "-" + e.getMessage());//Throws nice output message 

            }
    }

        private static void CharacterCounter() throws IOException{

        FileInputStream fstream = new FileInputStream("/Users/Devonte1/Desktop/Javatest.txt");//Read in file

        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));//Take file stream and place it into bufferedReader
        OutputStreamWriter bw = null;

        String strLine="";
        String removeSpace="";
        while ((strLine = br.readLine()) != null) {

            removeSpace+=strLine;
        }

        String st=removeSpace.replaceAll(" ", "");//Replace all spaces 
        char[]text = st.toCharArray();//Create new conjoined character array
        System.out.println("Character Total");

        int [] count = new int [256];//Character array

        //Create index 
            for(int x = 0; x < 256; x ++){
                    count[x]=0;
            }

        //Search file 
        for (int index = 0; index < text.length; index ++) {
             char ch = text[index];
             int y = ch;
             count[y]++;
        }

        //
        for(int x = 0; x < 256; x++){
            char ch= (char) x;
            if (count[x] == 0){ 
                System.out.println("Character not used"+ " "+ ch + " = (char code " + (int) ch + ")");
            }
            else if (count[x] != 0){
                System.out.println("Character " + ch + " used" + count[x] + " = (char code " + (int) ch + ")");
            }
        }

        }

}

错误:

Error:Arrayoutofboundexception: 8217

我做错了什么?

【问题讨论】:

  • 请添加完整的堆栈跟踪。
  • 鉴于 char is defined as Unicode character, which implies 16 bits unsigned 为什么你的范围是 256?
  • 你为什么使用DataInputStream?您应该只使用DataInputStream 来读回最初使用DataOutputStream 写入的文件!只需使用普通的“FileInputStream”即可。
  • count 是 256 个元素,但 count[y]++ 使用输入文件中的 char 的值进行索引。 chars 的值最高可达 65,536,因此您很容易受到 ArrayIndexOutOfBoundsException 的影响。
  • 那是您文本中正确的单引号字符。

标签: java arrays exception counter


【解决方案1】:

解决方案 1

统计所有 65,535 个字符。 需要将 count 数组的大小更改为长度为 65,535:

int [] count = new int [65535];  // Character array

// Create index 
for (int x = 0; x < 65535; x ++){
  count[x] = 0;
}

在打印统计信息时,将最后一部分的 256 更改为 65535。

解决方案 2

仅对序数值小于 256 的字符进行计数统计:

// Create index 
for(int x = 0; x < 256; x ++){
  count[x] = 0;
}

// Search file 
for (int index = 0; index < text.length; index ++) {
  char ch = text[index];
  int y = ch;
  if (y < 256)
    count[y]++;
}

【讨论】:

  • char 值是 UTF-16 代码单元,所以我有点不愿意称它们为“字符”。它们的值范围从Character.MIN_VALUECharacter.MAX_VALUE。所以,是的,int[Character.MAX_VALUE + 1] 会起作用。 (0 到 127 将是 C0 Controls 和 Basic Latin 块的完整代码点。0 到 255 将添加 C1 Controls 和 Latin-1 Supplement 块。请参阅Unicode)但是尝试“?”。
  • 谢谢。在 ADABAS 字符集中,最多 256 个字符只会出现在数据中,这是程序将循环遍历的文件类型,因此在这种情况下 256 个就足够了。
  • 好的。 @DeCampbell 如果解决方案适合您,请标记“已回答”。
  • 嗨,我不知道该怎么做?
  • @DeCampbell 很有趣,但是这 256 个字符总是U+0000 to U+00FF 吗? (如果是这样,那将与 ISO 8859-1 相同。)而且,由于 FileInputStream 始终使用 Java 视为用户系统默认值的字符编码,这是否也与 ADABAS 匹配?注意:Java 不像 C 那样“灵活”,其中 char 以您希望的任何字符编码保存值,甚至是自定义编码,甚至是非文本数据。
猜你喜欢
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2014-03-05
  • 2014-07-04
  • 2017-10-14
  • 2021-08-15
  • 2020-07-27
相关资源
最近更新 更多