I'm creating a program that simple creates objects of soccer teams and then stores the names and records of the teams in a text file.
I'm trying to create sections in my file however so I can print out teams that are on the same continent.
Pretend the next line of text is the beginning of my text file
North America
End North America
Hey guys
If I want my objects of teams who are in North America only to be written in between those two lines of text how would I do that?
The results I have gotten so far aren't working correctly, the team name and records are being printed right after "End North America".
Below is my code thus far.
import java.util.*;
import java.io.*;
public class Teams {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner nameInput = new Scanner(System.in);
System.out.println("Team Creator!");
System.out.println("-----------------------------");
System.out.println("Select an option");
System.out.println("");
System.out.println("1. Create-A-Team");
System.out.println("");
int optionSelection = input.nextInt();
if(optionSelection==1) {
System.out.println("Please enter the name of the team you'd like to create");
String teamName = nameInput.nextLine();
Team team = new Team();
team.createTeam(teamName);
}
}
}
class Team {
String name;
int wins,loses;
boolean northAmericanTeam = true;
public void createTeam(String name) {
Team newTeam = new Team();
newTeam.name = name;
this.wins = 0;
this.loses = 0;
System.out.println("You created " + name + "!");
System.out.println("Wins: " + wins);
System.out.println("Loses: " + loses);
if(northAmericanTeam == true) {
BufferedReader reader = null;
try(PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter("teaminfo.txt", true)))) {
File file = new File("teaminfo.txt");
reader = new BufferedReader(new FileReader(file));
String line;
String nAmerica ="North America";
while ((line = reader.readLine()) != null) {
System.out.println(line);
if(line.equals(nAmerica)) {
write.println(name+"" + ": Wins[" +wins+"] Loses[" +loses+"]");
write.close();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire