Makri
Makri

Reputation: 341

Getting error on printing array

I got this code:

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);
    }
    }
}

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?

Upvotes: 0

Views: 2370

Answers (4)

ppeterka
ppeterka

Reputation: 20726

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

[Ljava.lang.String;@163de20

Means:

  • array of references ( [L )
  • of type String ( java.lang.String )
  • unique object ID

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

Upvotes: 1

Dom
Dom

Reputation: 1722

You have to print the array element by element.

ie.

    for(int i = 0; i < ordArray.length; i++)
        System.out.println(ordArray[i]);

Upvotes: 0

arshajii
arshajii

Reputation: 129507

Actually this is commonly considered to be a "mistake" of arrays in Java: arrays don't override toString(), sadly. What you see is Object's toString():

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

A common workaround is to use Arrays.toString():

System.out.println(Arrays.toString(oldArray));

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Use Arrays.toString() to log your Array's contents

System.out.println(Arrays.toString(ordArray));

If you want a formatted output you need to iterate over it using a good old for loop.

for (int i = 0; i < ordArray.length; i++) {
   System.out.printf("ordArray[%d] = %s", i, ordArray[i]);
}

Upvotes: 0

Related Questions