Scanner skipping every second line from file [duplicate]

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);

Leave a Comment