Input technique for unknown lines of input
I have a problem with input techniques from user. Suppose user can feed up program 3 lines as:
Code:
Department of Computer science
and
engineering University of Dhaka
But our programmer does not know how many lines are there and we have to process one line and then to next line. After reading all the lines i have to display all inputs accordingly. Output should be
Code:
Department of Computer science
and
engineering University of Dhaka
Here is my program
Code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class InputTechnique {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
ArrayList<String> aList = new ArrayList<String>();
while(scan.hasNextLine()){
aList.add(scan.nextLine());
}
Iterator it = aList.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
My program hangs on first while loop. How can solve this problem?