The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to design two programs in java
1) A 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.txt (each record will have a field for the player's name and a field for the player's score, separated by a comma)
Enter a player's name: Arnold Palmer Enter the player's score 74 Do you want to enter another record? Enter y for yes or anything else for no: y ... Store the data as comma separated records. For example: Arnold Palmer,74 Tiger Woods,69 Sam Snead,89 Micky Mouse,113 NOTE: On a windows machine, the file may be read with notepad. Other operating systems will have similar text editors to check the result.
2) A program that reads the records from the golf.txt file and displays them.
Quick and dirty table without a header
Example Output:
Player Name: Arnold Palmer Score: 74 Player Name: Tiger Woods Score: 69 Player Name: Sam Snead Score: 89 Player Name: Micky Mouse Score: 113
Solution for Question 1
import java.util.*;
import java.io.FileWriter; // Import the FileWriter class
import java.io.IOException; // Import the IOException class to
handle errors
public class Main
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
char choice; // to hold 'y' or 'n'
// try catch for caught error
try {
// creating object from
FileWriter
FileWriter myWriter = new
FileWriter("golf.txt",true);
do{
// calling namescore function to save user name and score into the
file
myWriter.write(nameScore()+"\n");// adding with new line
// promting user for yes or no
System.out.println("Do you want to enter another record?\nEnter y
for yes or anything else for no:");
// taking choice from user
choice = obj.next().charAt(0);
}
while ((choice == 'y') || (choice == 'Y'));
// closing file
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("Error: "+e);
e.printStackTrace();
}
}
// method to take name and socre as input from user
public static String nameScore(){
Scanner obj = new Scanner(System.in);
System.out.println("Enter a player's name: ");
String name=obj.nextLine();
System.out.println("Enter the player's score: ");
int score = obj.nextInt();
String str=name+","+score;
// return string
return str;
}
}
Question 2 Solution :
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws IOException
{
FileReader frObj = new FileReader("golf.txt");
Scanner inFile = new Scanner(frObj);
// Read till end of file
while (inFile.hasNext())
{
// reading every line
String line = inFile.next();
// deviding line as words using delemeter comma(,) using split
method and saving into string Array
String[] arrOfStr = line.split(",", 2);
// displaying name and score
System.out.print("Player Name: " + arrOfStr[0]);
System.out.print(" Score: " + arrOfStr[1]);
System.out.println();
}
inFile.close();
}
}
program Screen shot
Get Answers For Free
Most questions answered within 1 hours.