Well for reading a file I use BufferedReader:
String fileName="C:/folder1/file.txt"
BufferedReader reader = new BufferedReader( new FileReader(fileName) );
String line = reader.readLine();
while(line!=null) {
System.out.println(line);
line = reader.readLine();
}
First you need to read the first line which happens outside the loop.
Then in order to enter the loop and continue, what you read should be not null. At the end of the loop after you have finished with all the calculations you read the next. Again you check to see if it is not null in order to continue. At the end of each loop you read the next line, so when you go at the begining you need to check if it is null or not in order to continue.
When you have reached the End Of the File the reader.readLine();
will return null so the while loop will exit.
Now after you have read the line here is an example on how to proceed
String line = "2 3";
String [] tokens = line.split(" ");
//tokens[0] has value "2"
//tokens[1] has value "3"
addPoints(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
As for question about entering parameters, if you run the program like this:
java MainClass C:/folder1/file.txt
Then inside the main, the args array will have size 1 and the args[0] will have the input you gave:
public static void main(String[] args) {
String file = args[0];
System.out.println(file);
}
Of course all the necessary checks should be made. Example:
public static void main(String[] args) {
String …