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).

1st Oct 2017, 9:31 AM
Aditya Kshitiz
Aditya Kshitiz - avatar
2 odpowiedzi
+ 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(" ");
1st Oct 2017, 9:57 AM
Maksym Zieliński
Maksym Zieliński - avatar
+ 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]); } } }
1st Oct 2017, 10:03 AM
Maksym Zieliński
Maksym Zieliński - avatar