Reputation: 132
I am making a program that reads data from a file called playoffs.txt. Each line has a team name, score, and division, each separated by colons, like so:
Panthers:0:Atlantic
Lightning:0:Atlantic
Red Wings:0:Atlantic
I want to run a for loop that prints out this for each team:
Team: Panthers
Series Score: 0
Division: Atlantic
However, I'm having trouble figuring out how to separate the values on each line, since readLine seems to print out the entire line. Here's my method:
private static void series(String team1) throws FileNotFoundException, IOException{
BufferedReader in = new BufferedReader(new FileReader("playoffs.txt"));
String[] team = new String[0];
int[] score = new int[0];
String[] division = new String[0];
String s;
int j = 0;
while((s = in.readLine()) != null){
String[] var = s.split(":");
team[j] = in.readLine(var[0]);
score[j] = in.read();
division[j] = in.read();
cout.println(team[j]);
cout.println(score[j]);
cout.println(division[j]);
j++;
}
}
I'm new to Java and the s.split line I found on another StackOverflow question that was similar to my question, and it runs an error saying "String cannot be converted to boolean." I'm also confused about the difference between readLine() and read(). Any help to point me in the right direction to read each value separately would be appreciated.
Upvotes: 0
Views: 1817
Reputation: 7868
You correctly split on the ':' symbol, which returns a String[]
.
You don't need to call in.readLIne(var[0])
. You already have the values in the String[]
. Simply assign them to your variables:
while((s = in.readLine()) != null){
String[] var = s.split(":");
team[j] = var[0];
score[j] = var[1];
division[j] = var[2]
cout.println(team[j]);
cout.println(score[j]);
cout.println(division[j]);
j++;
}
This solves one of your issues. However, since you are trying to store multiple values per entry, you are better off with a container class that represents the data you have. You can then create a new instance of the container class and override the toString() method to print the contents of each after all input is received.
Try adding a GameResult class (or call it something else):
public class GameResult{
String team;
int score;
String division;
public GameResult(String team, int score, String division){
this.team = team;
this.score = score;
this.division = division;
}
public String getTeam(){
return team;
}
public int getScore(){
return score;
}
public String getDivision(){
return division;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Team: ").append(team).append("\n");
sb.append("Score: ").append(score).append("\n");
sb.append("Division: ").append(division).append("\n");
return sb.toString();
}
}
Now you can use the new class and for each line store in a List to later print the results:
private static void series(String team1) throws FileNotFoundException, IOException{
BufferedReader in = new BufferedReader(new FileReader("playoffs.txt"));
List<GameResult> results = new ArrayList<>();
String s;
while((s = in.readLine()) != null){
String[] var = s.split(":");
GameResult result = new GameResult(var[0], var[1], var[2]);
}
//Print out the results
for(GameResult aResult: results){
System.out.print(aResult);
}
}
Upvotes: 1
Reputation: 44854
try
String[] var = s.split(":");
team[j] = var[0];
score[j] = var[1];
division[j] = var[2];
but you should initialise your team
to be big enough to hold your data.
I would personally use an ArrayList
Consider creating a new class called Team
which has a team name, a score and a division. The you can create an ArrayList <Team> = new ArrayList <Team> ();
Upvotes: 3