java - Printing odd lines - line counter incremented two times -
i made simple program, print odd lines file.
public static void main(string[] args) throws filenotfoundexception{ file file = new file("text.txt"); scanner fileread = new scanner(file); int linecount = 0; int = 0; while(fileread.hasnextline()){ linecount++; = linecount % 2; system.out.println("line count -- >> " + linecount); if(i == 1){ system.out.println(fileread.nextline()); } } fileread.close(); } } so when run it, output
line count -- >> 1
odd
line count -- >> 2
line count -- >> 3
even
line count -- >> 4
line count -- >> 5
odd
and on .... why linecount incremented 2 times ? in advance
public static void main(final string[] args) throws filenotfoundexception { final file file = new file("c:\\textstr.txt"); final scanner fileread = new scanner(file); int linecount = 0; int = 0; while (fileread.hasnextline()) { linecount++; = linecount % 2; final string str = fileread.nextline(); if (i == 1) { system.out.println("line count -- >> " + linecount); system.out.println(str); } } fileread.close(); }
Comments
Post a Comment