Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Buffered Reader is not reading my file properly... HELP!

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Buffered Reader is not reading my file properly... HELP!

    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?
    Last edited by helloworld922; November 8th, 2009 at 03:52 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    public String toString()
    {
         return AwayTeam + ": " + awayScore + "\n" + HomeTeam + ": " + homeScore;
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    mannyT (November 8th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Buffered Reader is not reading my file properly... HELP!

    You should put that code inside your Match class.

  6. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  8. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buffered Reader is not reading my file properly... HELP!

    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
    Last edited by helloworld922; November 8th, 2009 at 08:07 PM.

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Buffered Reader is not reading my file properly... HELP!

    Ahh. You forgot the function signature for the toString() method

    public String toString()

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    mannyT (November 8th, 2009)

  11. #9
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buffered Reader is not reading my file properly... HELP!

    ooo thanks for all your help!! It workeddd!

Similar Threads

  1. Reading doubles from file
    By fawx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 25th, 2009, 12:57 AM
  2. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 04:45 AM
  3. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 07:11 PM
  4. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 04:51 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 10:31 AM