Buffered Reader is not reading my file properly... HELP!
Code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class match {
private static class Match {
private int HomeScore = 0;
private int AwayScore = 0;
private String HomeTeam;
private String AwayTeam;
public void setAwayScore(int AwayScore) {
// TODO Auto-generated method stub
this.AwayScore = AwayScore;
}
public void setHomeScore(int HomeScore) {
// TODO Auto-generated method stub
this.HomeScore = HomeScore;
}
public void setAwayTeam(String AwayTeam) {
// TODO Auto-generated method stub
this.AwayTeam = AwayTeam;
}
public void setHomeTeam(String HomeTeam) {
this.HomeTeam = HomeTeam;
}
}
public static void main(String[] args) throws Exception{
final List<Match> matches = new ArrayList<Match>();
final BufferedReader BufferedReader = new BufferedReader(new BufferedReader(new FileReader("results.txt"))); //read the list results file
int CountValid = 0;
int CountInvalid = 0;
int CountGoals = 0;
if (BufferedReader != null){ //if not equal to null
String text;
while((text = BufferedReader.readLine()) != null ){
String[] split = text.split(":"); //split by : marks
if (split.length >= 4) { //split the file by :
final Match match = new Match();
match.setHomeTeam((split[0]).trim()); //this sets the first part of home team name
match.setAwayTeam((split[1]).trim()); //this sets the second part of away team name
match.setHomeScore(Integer.parseInt(split[2].trim())); //this sets the third part of home team name
match.setAwayScore(Integer.parseInt(split[3].trim())); //this sets the fourth part of away team name
matches.add(match); //adds all parts to the match
CountValid++;
CountGoals+= match.HomeScore + match.AwayScore; //adds home and away goals
}
if (split.length <= 3) { //if 3 or less
CountInvalid++; // adds invalid count
}
//BufferedReader.close();
}
if (!matches.isEmpty()) {
for (final Match match : matches){
if(match.HomeTeam.length() == 0) //if home team has 0 characters
{CountInvalid ++;CountValid --;
CountGoals = CountGoals -(match.HomeScore + match.AwayScore); }
else if(match.AwayTeam.length() ==0 )
{CountInvalid ++; CountValid--; CountGoals = CountGoals -(match.HomeScore + match.AwayScore); }
else {
System.out.println(match.toString());
}
}
System.out.println("The Total Number of Goals was" +CountGoals);
System.out.println("The Total Number of Valid games were" +CountValid);
System.out.println("The Total Number of invalid games were" +CountInvalid);
}
}
}
}
Instead of reading my file, it says match$Match@1d58aae and so on. What is wrong within my codeee?
Re: Buffered Reader is not reading my file properly... HELP!
You haven't over-ridden the toString() method of Match. By default, I believe the toString method returns the memory address of the object if it isn't over-ridden.
Code :
public String toString()
{
return AwayTeam + ": " + awayScore + "\n" + HomeTeam + ": " + homeScore;
}
Re: Buffered Reader is not reading my file properly... HELP!
Thanksss.. i added tht code but it did not work. You are rightt it is showin the memory address. It is saying that the 'void methods can not return a value'. What does this mean? Wheres about within my code to do include the return string?
Re: Buffered Reader is not reading my file properly... HELP!
You should put that code inside your Match class.
Re: Buffered Reader is not reading my file properly... HELP!
Yehh i have done that but its called getin underlined in red and it says that ''Void methods can not return a value". Why does this happen? please help :)
Re: Buffered Reader is not reading my file properly... HELP!
Post your updated code. There's nothing in your previous code that would cause that.
Re: Buffered Reader is not reading my file properly... HELP!
Code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class match {
private static class Match {
private int HomeScore = 0;
private int AwayScore = 0;
private String HomeTeam;
private String AwayTeam;
public void setAwayScore(int AwayScore) {
// TODO Auto-generated method stub
this.AwayScore = AwayScore;
}
public void setHomeScore(int HomeScore) {
// TODO Auto-generated method stub
this.HomeScore = HomeScore;
}
public void setAwayTeam(String AwayTeam) {
// TODO Auto-generated method stub
this.AwayTeam = AwayTeam;
}
public void setHomeTeam(String HomeTeam) {
this.HomeTeam = HomeTeam;
}
{
return AwayTeam + ": " + AwayScore + "\n" + HomeTeam + ": " + HomeScore;
}
}
public static void main(String[] args) throws Exception{
final List<Match> matches = new ArrayList<Match>();
final BufferedReader BufferedReader = new BufferedReader(new BufferedReader(new FileReader("results.txt"))); //read the list results file
int CountValid = 0;
int CountInvalid = 0;
int CountGoals = 0;
if (BufferedReader != null){ //if not equal to null
String text;
while((text = BufferedReader.readLine()) != null ){
String[] split = text.split(":"); //split by : marks
if (split.length >= 4) { //split the file by :
final Match match = new Match();
match.setHomeTeam((split[0]).trim()); //this sets the first part of home team name
match.setAwayTeam((split[1]).trim()); //this sets the second part of away team name
match.setHomeScore(Integer.parseInt(split[2].trim())); //this sets the third part of home team name
match.setAwayScore(Integer.parseInt(split[3].trim())); //this sets the fourth part of away team name
matches.add(match); //adds all parts to the match
CountValid++;
CountGoals+= match.HomeScore + match.AwayScore; //adds home and away goals
}
if (split.length <= 3) { //if 3 or less
CountInvalid++; // adds invalid count
}
//BufferedReader.close();
}
if (!matches.isEmpty()) {
for (final Match match : matches){
if(match.HomeTeam.length() == 0) //if home team has 0 characters
{CountInvalid ++;CountValid --;
CountGoals = CountGoals -(match.HomeScore + match.AwayScore); }
else if(match.AwayTeam.length() ==0 )
{CountInvalid ++; CountValid--; CountGoals = CountGoals -(match.HomeScore + match.AwayScore); }
else {
System.out.println(match.toString());
}
}
System.out.println("The Total Number of Goals was" +CountGoals);
System.out.println("The Total Number of Valid games were" +CountValid);
System.out.println("The Total Number of invalid games were" +CountInvalid);
}
}
}
}
Here is my updated codee. Can you sort it out so its works for me pleaseee
Re: Buffered Reader is not reading my file properly... HELP!
Ahh. You forgot the function signature for the toString() method
Re: Buffered Reader is not reading my file properly... HELP!
ooo thanks for all your help!! It workeddd! :)