0

I'm trying to convert a string into an array of tokens, and while I've been finding questions similar to this all over the internet, no one is explaining how to specifically convert the words into a string. For example, I might do this:

    String a = "I am a unicorn.";
    String b[] = a.split(" ", -1);

    for (int i = 0; i < b.length; i++)
        System.out.println(b);

When I run this, however, it prints this:

[Ljava.lang.String;@15db9742

[Ljava.lang.String;@15db9742

[Ljava.lang.String;@15db9742

[Ljava.lang.String;@15db9742

I'm finding, all over the internet, this exact same code, yet I'm not finding anyone explaining how to fill the array with the tokens that I specified in my string.

(Also, please don't virtually "yell" at me for my variables! :) A lot of times when I ask questions about Java, people prefer to respond with things like, "a and b aren't proper variables, you should use more specific words", rather than answering my question. But keep in mind, this is just an example, so you guys can get an idea of what I'm talking about - I use all of the proper rules of coding in every other situation.)

3
  • This link might help you - howtodoinjava.com/java/string/… Commented Feb 12, 2020 at 6:02
  • 1
    "Also, please don't virtually "yell" at me for my variables!", maybe you should just start to name them better, then you would for this example notice yourself that you are printing the array object instead of individual strings that the array contains. Commented Feb 12, 2020 at 6:40
  • Proper variable naming helps everyone who reads the code, which means you are concerned about people who read the code for answering your question, so in any case whether in a actual project or while asking questions on SO, proper variable naming is a good practice. Commented Feb 12, 2020 at 6:41

1 Answer 1

2

Every time you are trying to print the whole array

try.

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

or even use a for-each loop

for (String el : b) {
    System.out.println (el);
}

ALSO CHANGE YOUR VARIABLE NAMES TO SOMETHING MORE MEANINGFUL

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

1 Comment

Oh, I forgot the i, that was the issue. Wow, I've spent months on this same issue, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.