Im a first year CS student and I am working on an assignment but I ran into this while loop that won't stop...
import java.io.*;
import java.util.Scanner;
class Ass1{
public static void main(String[] args){
int i=0;
try{
File myfile = new File ("./ages.txt");
Scanner scanner = new Scanner(myfile);
while(scanner.hasNext()){
i++;
}
System.out.println("ages.txt contains " + i + " lines");
scanner.close();
}catch(IOException e){}
}
}
and the ages.txt file looks like the following (They are meant to be all in separate lines but somehow I can only show them in a line here :( )
200 201 202 203 205 205 207 208 210 213 214 217 218 219 219 221 225 226 227 227 231 232 238 238 240 309 313 314
I am trying to read all the lines from the text file and at the end print how many lines it contains.
Thank you in advance for your help.
hasNextLine().