简体   繁体   中英

Java String split by comma

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


}

Change this

   System.out.println(arr);

to

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

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

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

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.

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

str = line.split(",");

如果字符串包含逗号,则不应使用split()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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