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?
1
Expert's answer
2012-05-24T08:55:50-0400
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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS