Scanner skipping every second line from file [duplicate]
Solution 1:
You are always reading the line twice (unless you get a *
)
if(scan.nextLine().equals("*")) // read here - "someString"
break;
words.add(scan.nextLine()); // ignore last read line and read again.
You read only once and then compare.
String value = scan.nextLine();
// check for null / empty here
if (value.equals("*"))
break;
words.add(value);
Solution 2:
You are reading it twice.
Store it, use it.
while(scan.hasNextLine()) {
String str = null;
if((str =scan.nextLine()).equals("*"))
break;
words.add(str);//here you are not reading again.
}