-1

Edit: I have modified the question for simplicity.

I have this method to split a long string into an array of strings and it shall return a double string, so that I can write it on a .csv file later:

public class Test {
    public static void main(String[] args) throws IOException {
        String test = "Michael Porter Jr. 2018-19 Panini Prizm Silver Prizm PSA 10 Gem Mint! Hot! ?? \n 2018 Prizm Silver MICHAEL PORTER JR. RC PSA 10 Gem Mint";
        System.out.println(test);
        String[] test_split = test.split("\\r?\\n");
        for (String t : test_split) {
            System.out.println(test_split.toString());
        }

    }

}

My code returns this:

Michael Porter Jr. 2018-19 Panini Prizm Silver Prizm PSA 10 Gem Mint! Hot! ?? 
 2018 Prizm Silver MICHAEL PORTER JR. RC PSA 10 Gem Mint
[Ljava.lang.String;@4a574795
[Ljava.lang.String;@4a574795

I actually expected to have an arrays with the 2 names (split by the newline character), but I got something weird. How may I fix this please?

DiMario
  • 33
  • 1
  • 8
  • *Unrelated:* Instead of `"\\r?\\n"`, use `"\\R"` – Andreas Mar 15 '20 at 00:02
  • 2
    I think you meant to print `t`, not `test_split`, inside that loop. – Andreas Mar 15 '20 at 00:03
  • Oh that's it! Thank you so much! Yes, I actually was looking for t. – DiMario Mar 15 '20 at 00:04
  • 1
    See also: [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784/5221149) – Andreas Mar 15 '20 at 00:04
  • BTW `split("\\r?\\n");` makes `\n` mandatory, so it will fail to split `Foo\rBar`. In case you don't know which line separator will appear in text consider using `split("\\R")` to represent all (or almost all, I can't be sure what will bring the future) line separators (more info https://stackoverflow.com/a/31060125). – Pshemo Mar 15 '20 at 00:13

3 Answers3

2

You seem to be printing out an Array of Strings not a String, judging by the output you posted, without really seeing in which way you actually call e.g. println. If this is really the case you could also use:

Arrays.toString(yourArray);
1

It might be better if you reversed the arrays. Make them a 2xN instead of an Nx2 Here is a segment of your method.

         String[][] nameAndPrice = new String[2][listingName_extract.length];

         for (int i = 0; i < listingName_extract.length; i++) {
              nameAndPrice[0][i] = String.valueOf(listingName_extract[i]);
              nameAndPrice[1][i] = String.valueOf(listingPrice_extract[i]);
          }
         return nameAndPrice;

And you should probably be using Arrays.toString() to print out the array.

System.out.println(Arrays.toString(nameAndPrice[0]);
System.out.println(Arrays.toString(nameAndPrice[1]);

Then if you want to print them side by side.

 for (int i = 0; i < listingName_extract.length; i++) {
     System.out.println(nameAndPrice[0][i] + " " + nameAndPrice[1][i]);
 }
WJS
  • 36,363
  • 4
  • 24
  • 39
0

As users TheThingAboveMe and Andreas suggested, I should be looking at t (the strings) and not the test_split (the array itself).

I have also edited my code per user WJS's suggestion.

Dharman
  • 30,962
  • 25
  • 85
  • 135
DiMario
  • 33
  • 1
  • 8