public class Test{
public static String[] intoArray(String input){
input = input.trim() + " ";
char word = ' ';
int num = 0;
for(int i=0; i < input.length(); i++){
if (input.charAt(i) == word){
num++;
}
}
String[] array = new String[num];
for(int i=0; i< array.length; ){
for(int j=0; j < input.length(); j++){
if(input.charAt(j) == word){
array[i] = input.substring(0, j);
input = input.substring(j + 1, input.length());
j=0;
i++;
}
}
return array;
}
return null;
}
}
Main:
public class Main
{
public static void main(String[] args)
{
String[] results = Test.intoArray("Test,putting,in,a,array");
for (String result : results)
{
System.out.println(result);
}
}
}
I am trying to put string text into an array and displaying it downwards one by one as an array, but my output is Test,putting,in,a,array. Does this mean I have not put the string into an array?