简体   繁体   中英

String to String Array Java

I have a big problem. I have just try to search a solution, but i don't have find any solution. i have this code

public static String getString() throws IOException {

    String content = null;
    File folder = new File("C:\\Soluzioni.txt");

    content = FileUtils.readFileToString(folder) + "\n";
    String remainingString = content.substring(content.indexOf("["),
            content.lastIndexOf("]") + 1);
    System.out.println(remainingString);
    return remainingString;

}

And this is ok. (For clarity)

OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44]

My problem is now:

public static String[] arg() throws IOException {
    String[] strArray = { getString() };
    System.out.println(strArray);
    return strArray;
}

when i print strArray, i have an error (ECLIPSE show me this: [Ljava.lang.String;@796686c8). I would need my string (remainingString) to become an array of strings(strArray), but that maintained the same format, that is, it was always OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44] but with array format. Thank you very much!

Arrays are objects too in Java, but they don't override Object 's toString() method , which is responsible for the output: [Ljava.lang.String;@796686c8

In other words, this method returns a string equal to the value of:

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

Use Arrays.toString to generate the expected array output:

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

You have to use Arrays.toString() method which returns a string representation of the contents of the specified array.
Change your system out to System.out.println(Arrays.toString(strArray));

System.out.println(strArray); will print the Object toString value getClass().getName() + '@' + Integer.toHexString(hashCode())

That's why you are getting [Ljava.lang.String;@796686c8) .

Please refer http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html for more information.

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