Question

Modify the method so that instead of manually making teams, it reads it from a text...

Modify the method so that instead of manually making teams, it reads it from a text file called TeamsList.txt. Everything must be done in this method.

public void make_test_teams() {
List<Team> teams = new ArrayList<Team>();

Player burns = new Player("George Burns", "George");
Player allen = new Player("Gracie Allen", "Gracie");
List<Player> ba = new ArrayList<Player>();
ba.add(burns); ba.add(allen);
Team burns_and_allen = new Team("Burns and Allen", ba);
teams.add(burns_and_allen);

Player abbott = new Player("William Abbott", "Bud");
Player costello = new Player("Louis Cristillo", "Lou");
List<Player> ac = new ArrayList<Player>();
ac.add(abbott); ac.add(costello);
Team abbott_and_costello = new Team("Abbott and Costello", ac);
teams.add(abbott_and_costello);

Player chico = new Player("Leonard Marx", "Chico");
Player groucho = new Player("Julius Marx", "Groucho");
Player harpo = new Player("Adolph Marx", "Harpo");
List<Player> mb = new ArrayList<Player>();
mb.add(chico); mb.add(groucho); mb.add(harpo);
Team marx_brothers = new Team("Marx Brothers", mb);
teams.add(marx_brothers);

store_teams(teams);
   }

the text file looks like

team: "Burns and Allen"
player: fullname: George Burns, nickname: George
player: fullname: Gracie Allen, nickname: Gracie

team: "Abbott and Costello"
player: fullname: William Abbott, nickname: Bud
player: fullname: Louis Cristillo, nickname: Lou

team: "Marx Brothers"
player: fullname: Leonard Marx, nickname: Chico
player: fullname: Julius Marx, nickname: Groucho
player: fullname: Adolph Marx, nickname: Harpo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Updated Method:

public void make_test_teams() {
   List<Team> teams = new ArrayList<Team>();

   // Reading data from file
   try {
       //Scanner class object to read data from file
       Scanner reader = new Scanner(new File("TeamsList.txt"));
      
       //Reading data from file
       while(reader.hasNext()) {
           String line1 = reader.nextLine();
           String line2 = reader.nextLine();
           String line3 = reader.nextLine();
          
           //Fetching Team name
           String teamName = line1.split(": ")[1]
           //Replacing double quotes
           teamName = teamName.replaceAll("^\"|\"$", "");
          
           //For player 1
           //Fetching full name
           String p1fullName, p1nickName;
           p1fullName = ((line2.split(", "))[0]).split(": ")[2]
           //Replacing comma
           p1fullName = p1fullName.replaceAll(",", "");
          
           //Fetching nick name
           p1nickName = ((line2.split(", "))[1]).split(": ")[1]
          
           //For player 2
           //Fetching full name
           String p2fullName, p2nickName;
           p2fullName = ((line3.split(", "))[0]).split(": ")[2]
           //Replacing comma
           p2fullName = p2fullName.replaceAll(",", "");
          
           //Fetching nick name
           p2nickName = ((line3.split(", "))[1]).split(": ")[1]
          
           //Creating objects
           Player p1 = new Player(p1fullName, p1nickName);
           Player p2 = new Player(p2fullName, p2nickName);
          
           List<Player> ba = new ArrayList<Player>();
           ba.add(p1); ba.add(p2);
          
           Team teamN = new Team(teamName, ba);
           teams.add(teamN);
       }
      
       store_teams(teams);
      
       //Closing file
       reader.close();
   }
   catch(IOException ex) {
       System.out.println(ex);
   }
   catch(FileNotFoundException ex) {
       System.out.println(ex);
   }
}

Add a comment
Know the answer?
Add Answer to:
Modify the method so that instead of manually making teams, it reads it from a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the method so that instead of manually making teams, it reads it from a text...

    Modify the method so that instead of manually making teams, it reads it from a text file called TeamsList.txt. Everything must be done in this method. public void make_test_teams() { List<Team> teams = new ArrayList<Team>(); Player burns = new Player("George Burns", "George"); Player allen = new Player("Gracie Allen", "Gracie"); List<Player> ba = new ArrayList<Player>(); ba.add(burns); ba.add(allen); Team burns_and_allen = new Team("Burns and Allen", ba); teams.add(burns_and_allen); Player abbott = new Player("William Abbott", "Bud"); Player costello = new Player("Louis Cristillo", "Lou"); List<Player>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT