2

I have this code down below that gives me this output

1,2,3,4,3,4,5,4,3,5,3,4,5,5,4,64,
[Ljava.lang.String;@3e25a5
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f

The input.txt file contains 1,2,3,4,3,4,5,4,3,5,3,4,5,5,4,64,

The code is this. It is clear there is a simple error in splitting but I find it hard to find what.

public static void main(String[] args) {
    // TODO Auto-generated method stub



    try{
        FileInputStream fstream = new FileInputStream("input.txt");
        DataInputStream  dat = new DataInputStream (fstream);
        BufferedReader in = new BufferedReader(new InputStreamReader(dat));
        String[] str ;

        int arr[] = new int [100];


        String line;

        while ((line = in.readLine()) != null)
            {

                System.out.println(line);
                str = line.split(",");
                System.out.println(str);

                for(int i = 0 ;i<str.length ; i++)
                {
                    arr[i]= Integer.parseInt(str[i]);
                    System.out.println(arr);
                }
            }

        fstream.close();

    }

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


}
2
  • What output do you want? Commented May 10, 2011 at 16:19
  • You're going to get a NumberFormatException because you have a trailing comma and the split() function is going to give you an empty string at the end of your array. Commented May 10, 2011 at 16:38

5 Answers 5

8

Change this

   System.out.println(arr);

to

   System.out.println(arr[i]);

before you were printing an array arr, not the array's elements.

Sign up to request clarification or add additional context in comments.

Comments

3

It looks like you're repeatedly printing the array, which will cause Java to call the array's toString() method, that will give you output like you've provided.

To fix it, try changing your loop to this:

for(int i = 0 ;i<str.length ; i++)
{
    arr[i]= Integer.parseInt(str[i]);
    System.out.println(arr[i]];
}

To give you a clearer idea what's going on, this program will do something similar:

public class Test {
    public static void main(String[] args) {
        System.out.println(new int[0]);
    }
}

It will output:

[I@164f1d0d

Comments

2

By printing out the array in

 System.out.println(arr);

you effectively print out an Object, which happens to be the Array object. That has a toString() method, and it tends to print out the Object's Class and the hashcode() reference.

Now, since your array is of a primitive type, it looks like this

[I@19821f

where each item means something

[ => "an array of"
I => "primitive integer"
@19821f => "which has a hashcode of 0x19821f"

Note that this is a default convention, meaning that while it is what you are seeing, you can't count on this reading technique with the same sense of the word guarantee.

Like many have mentioned before, you need to print out the element of the array (which won't use this pseudo-object toString() representation, because the element is an integer.

Comments

0

remove the unnecessary sysouts to get the exact output. and in last sysout print the element not complete array.

Comments

-2
str = line.split(",");

If the string contains comma, then you should not use split()

1 Comment

-1: This makes no sense, and seems completely wrong. If there's a comma in the string, that's no reason not to use split...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.