I have this piece of code that is supposed to add String values to an arrayList through the Scanner's next() or nextLine() method. The problem with next() is that it ignores everything after the first white space so I get that I should use the nextLine() method instead. The problem with nextLine() is that that it doesn't record the input, instead it stores a couple of blank spaces into the arrayList. Here's the code:
System.out.println("\nWhat is your idea? ");
String i = in.nextLine();
in.nextLine();
meals.add(i);
System.out.println("\n" + i + " has been entered into the idea pool. \n");
System.in.read();
I added the extra "in.nextLine()" after the initial "String i = in.nextLine()" because that's the only fix I found when I researched this problem but it doesn't work for me, it just still just stores a couple of blank spaces. Also, the System.in.read();
at the end there is only there so that it doesn't just jump forward after taking the input.
Here is the code where the above sample fits into:
ArrayList<String> meals = new ArrayList<String>();
String select = "";
while(!select.equals("")){
System.out.println("What would you like to do?");
System.out.println("1. <Irrelevant>");
System.out.println("2. Enter an idea");
System.out.println("3. <Irrelevant>");
System.out.println("4. <Irrelevant>");
System.out.println("Q. <Irrelevant>");
select = in.next();
switch(select){
case "1":
//Some stuff here.
case "2":
//Here's where the above problem fits into.
case "3":
//More stuff here
//and so on...
}
}