64 638
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #9934 in Java | JSP | JSF for Eileen Tucker

Question #9934
How can i use stdin to gather a string separated by spaces and add each element to an arraylist of strings?
Expert's answer
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Question9934 {

private static List<String> list = new ArrayList<String>();


public static void main(String[] args) {
&
& BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
& try{
& System.out.println("Please, input array of string, use spaces");
& String str = br.readLine();
& String[] array = str.split(" ");
&
& if(array.length == 0){
& System.out.println("Wrong array of strings!");
& System.exit(0);
& }
& for(int i=0; i<array.length;i++){
& list.add(array[i]);
& System.out.println(array[i]);
& }
&
&
& }
& catch(Exception e){
& e.printStackTrace();
& }

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions