Show code for a GolfTeam public method named readRoster that has a void return t
ID: 3889037 • Letter: S
Question
Show code for a GolfTeam public method named readRoster that has a void return type but has one String parameter containing the name of an input file. The method should populate the GolfTeam object with the data in the input file. The format for the input file is shown in the included text box (20pts) D. Team Name School Name golfer 1 first name golfer 1 last name golfer l handicap golfer 2 first name golfer 2 last name golfer 2 handicap public statre void readRostedStng fleN continues to the end-of-file E. Show code for a Golf eam public method named toString that has no parameters but returns a String object containing all the GolfTeam data in a format that matches the input file format (6pts).Explanation / Answer
GolfTeam.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GolfTeam {
static String s="";
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the file name: ");
String fileName = scan.next();
readRoster(fileName);
System.out.println(s);
}
public static void readRoster(String fileName) throws FileNotFoundException {
File file = new File(fileName);
if(file.exists())
{
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
s+=scan.nextLine()+" ";
}
} else {
System.out.println(fileName+" file does not exist");
}
}
public String toString() {
return s;
}
}
Output:
Enter the file name:
D:\golfteam.txt
Team Name
School Name
golfer 1 first name
golfer 1 last name
golfer 1 handicap
golfer 2 first name
golfer 2 last name
golfer 2 handicap
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.