打印阵列时出错 [英] Getting error on printing array

查看:90
本文介绍了打印阵列时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这段代码:

import java.util.*;
import java.io.*;

public class Oblig3A{
    public static void main(String[]args){
    OrdAnalyse O = new OrdAnalyse();
    OrdAnalyse.analyseMet();
    }
}

class OrdAnalyse {
    public static void analyseMet() {
    Scanner Inn = new Scanner(System.in);

    try {
        File skrivFil = new File("Opplysning.txt");
        FileWriter fw= new FileWriter(skrivFil);
        BufferedWriter bw = new BufferedWriter(fw);

        Scanner lesFil = new Scanner("Alice.txt");
        int i=0;
        int totalOrd=0;
        int antUnikeOrd=0;

        String[] ordArray = new String[5000];
        int[] antallOrd = new int[5000];

        while(lesFil.hasNext()) {
        String ord = lesFil.next().toLowerCase();
        totalOrd++;
        boolean ut=false;
        int y=0;
        int z=0;

        for(i=0; i<ordArray.length; i++) {
            if (ord.equals(ordArray[i])) {
            antallOrd[i]++;
            ordFraFor=true;
            }
        }
        if(ordFraFor=false) {
            antUnikeOrd++;
            z=0;
            boolean ordOpptelling=false;

            while(ordOpptelling=false) {
            if(ordArray[z] == null) {
                ordArray[z] = ord;
                antallOrd[z]++;
                ordOpptelling=true;
            }
            z++;
            }
        }
        }



        System.out.println(ordArray);

            }catch (Exception e){
        System.out.print(e);
    }
    }
}

这应该做的一个接一个地从文件中读出单词时有些重复计算。但是,当我最终尝试将数组打印到终端时只检查它是否正常,在我开始使程序能够将其写入文本文件之前,它只会给出一个错误:
[Ljava.lang.String; @ 163de20
但我不知道在这种情况下如何以及在何处检查错误?有什么帮助吗?

And this is supposed to do some heavy counting while reading the words out of a file one by one. However, when I finally try to print the array to terminal just check whether it is okay or not, before I start working on making the program able to write it to a text-file, it just gives an error which reads: [Ljava.lang.String;@163de20 But I do not know how and where to check for errors in this case? Any help?

推荐答案

这不是错误......这是Object类的默认toString()实现返回的...

This is not an error... This is what the default toString() implementation of the Object class returns...

[Ljava.lang.String;@163de20

意味着:


  • 引用数组( [ L

  • 类型为String( java.lang.String

  • 唯一对象ID

Object.toString代码()

Code of Object.toString()

public String toString() {
  return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

你应该做的是使用正确的方式打印:

What you shouldd do is to use a proper way to print:


  • 一个循环

  • a loop

StringBuilder sb = new StringBuilder();
for(String s: myArray) {
    sb.append(s);
    if(sb.length()>0) {
       sb.append(',');
    }
}
System.println(s.toString());


  • Arrays.toString

    这篇关于打印阵列时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆