PLEASE HELP ASAP in java please The Springfork Amateur Golf Club has a tournamen
ID: 3762357 • Letter: P
Question
PLEASE HELP ASAP in java please
The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to design two java programs. (1) A java program that will read each player's name and golf score as keyboard input, and then save these as records in a file named golf.dat. (Each record will have a field for the player's name and a field for the player's score.) (2) A java program that reads the records from the golf.dat file and displays them. It also displays the name of the player with the best (lowest) golf score.
Explanation / Answer
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class PlayerStore {
public static void main(String args[]){
saveFile();
readFile();
}
public static void readFile () {
String fileName = "players.dat";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
//String lineArray[] = line.split(" ");
}
// Always close files.
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
public static void saveFile () {
try {
FileOutputStream fos = new FileOutputStream ("players.dat", true);
DataOutputStream dos = new DataOutputStream (fos);
do{
Scanner scanner=new Scanner(System.in);
System.out.print("Player Name :");
String playerName=scanner.nextLine();
System.out.print("Player Score :");
int gulfScore=scanner.nextInt();
dos.writeUTF(playerName+" "+gulfScore+" ");
System.out.print("Enter any More(y for 1/n for 0) :");
int choice = scanner.nextInt();
if (choice == 0) {
break;
}
}while(true);
dos.close();
fos.close();
}
catch (IOException ioe) {
}
}
}
out put :
Player Name :dd
Player Score :34
Enter any More(y for 1/n for 0) :1
Player Name :erd
Player Score :45
Enter any More(y for 1/n for 0) :0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.