I want to take string as an input from user until "#" is the input i.e a series of string (all in different lines) till "#" is found and store the strings in an arraylist of type string. I wrote this code in JAVA but it's not working as required.
import java.util.ArrayList; import java.util.Scanner;
public class wordMetaMorphism {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> input = new ArrayList<String>();
while(!(sc.next().equals("#"))) {
String s = sc.next();
input.add(s);
}
System.out.println(input);
}
}
and i am getting this output where only the alternate strings are getting stored. I also tried sc.nextLine() but it is all same.
input - dip lip mad map maple may pad pip pod pop sap sip slice slick spice stick stock # #
output - [lip, map, may, pip, pop, sip, slick, stick, #]