0
How to store String inputs into array?
Suppose I have got an input "I like Java" then how can I separate these three words into an array. The user will not press enter key every new word in a sentence but will press space. (It should be a single line).
2 Answers
+ 1
There is a magic function in Java called split, that splits a String into array, by the sign that you use as argument. Like:
String input = "I like Java" ;
String[] strArray = input.split(" ");
+ 1
Full code for test:
public class Program
{
public static void main(String[] args) {
String input = "I like Java" ;
String[] strArray = input.split(" ");
for(int i = 0; i < strArray.length; i++ ){
System.out.println(strArray[i]);
}
}
}