0
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?

1 Answer 1

1

You currently are only getting one entry in your array because your words are separated by a comma, not a space. But, it's easy to fix change

char word = ' ';

to

char word = ',';

in intoArray. After that, I get (as I think you expected),

Test
putting
in
a

It is worth noting that in real code, String.split(String) is the right way to do it;

String[] results = "Test,putting,in,a,array".split(",");
Sign up to request clarification or add additional context in comments.

1 Comment

Oh okay I understand, thank you. I see, I was trying to put them into a string without the usage of split

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.