I’m trying to take string in input using Scanner.
But it asks me more strings that expected: shouldn’t with this code ask 5 strings?
public void go()
{
Scanner sc=new Scanner(System.in);
ArrayList<String> list=new ArrayList<String>();
String temp=new String();
Integer i=new Integer(0);
while(sc.hasNextLine() && i<5)
{
temp=sc.nextLine();
list.add(list.size(),temp);
i++;
}
}
If I try to run it it asks me 6 strings before the console stops to take input.
But i at the beginning is zero, it gets incremented 5 times before becoming 5.
So why it also remain in while when i is 5?
Solved: Both methods solved the problem.
If
i == 5still asc.HasNextLine()is called. Repair:P.S. do
int i = 0;