I'm trying to write a code that will read the contents of a file and I'm able to read it successfully. What I'm also trying to do is maybe declare a static constant like max_number_of_players so that the data read does not exceed this. Is there a way that I can do this? My code so far:
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args) throws IOException
{
String fileName = "Data/players.txt";
File file = new File(fileName);
Scanner in = new Scanner(file);
while(in.hasNextLine()){
String line = in.nextLine();
System.out.println(line);
}
in.close();
}
}
Just add another condition to the while-loop:
int playerCount = 0;
while(in.hasNextLine() && playerCount++ < max_number_of_players){
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.