I am trying to set arrays based on user input but I have an main issue. Here is my code:
Scanner myScanner = new Scanner(new BufferedInputStream(System.in));
System.out.println("Please enter your text");
ArrayList myArray = new ArrayList();
while (myScanner.hasNext()){
myArray.add(myScanner.nextLine());
for (int i = 0; i <myArray.size() ; i++) {
System.out.println(myArray.get(i));
}
}
When I insert
a
b
c
I just get
a
a
b
Also is there a better way to print my arrays?
Edit:
When I change my code like this
while (myScanner.hasNext()){
myArray.add(myScanner.nextLine());
break;
}
for (int i = 0; i <myArray.size() ; i++) {
System.out.println(myArray.get(i));
}
I just get a as a result.