Write a program that reads the records from the golftxt file
Write a program that reads the records from the golf.txt file written in Exercise
10a and prints them in the following format:
Name: Emily
Score: 30
Name: Mike
Score: 20
Name: Jonathan
Score: 23
Solution
ReadAndDisplay.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadAndDisplay {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File(\"D:\\\\golf.txt\");
if(inputFile.exists()){
Scanner scan = new Scanner(inputFile);
while(scan.hasNext()){
System.out.println(\"Name: \"+scan.next());
System.out.println(\"Score: \"+scan.next());
}
}
else{
System.out.println(\"Input file does not exist\");
}
}
}
Output:
Name: Emily
Score: 30
Name: Mike
Score: 20
Name: Jonathan
Score: 23
