-2

this like a string array {hi, hi, hello}, but if I have a string that is shaped like that, can i do this by entering a string variable into the string array ?

like this:
String animal = "kangaroo, rabbit, dog, cat, elephant";
String[] array = { animal };

the result is:
array[0] : kangaroo
array[1] : rabbit

  • Did you try to search the solution first before posting your question? A simple search would have given you hundreds of links with the required solution. – Arvind Kumar Avinash Jun 01 '20 at 17:25
  • Another one https://stackoverflow.com/questions/10631715/how-to-split-a-comma-separated-string – Arvind Kumar Avinash Jun 01 '20 at 17:28
  • Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – HC Tang Jun 01 '20 at 17:32

1 Answers1

1
   String animal = "kangaroo, rabbit, dog, cat, elephant";
   String[] array = animal.split(", ");
   System.out.println(Arrays.toString(array));
Debapriya Biswas
  • 907
  • 7
  • 18