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 2 of 2

Thread: array/string problem

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

    Default array/string problem

    i have some code which reads from a .txt file and picks up PL football teams from the file... and sends them to a String... how can i make it so i get only one team once i.e no teams repeat?

    this is my code:
     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
     
    public class generatortest {
     
        private static class Team{
     
            private String team;
    		int GamesPlayed;
    		int GoalDifference;
    		int GamesWon;
    		int GamesDrawn;
    		int GamesLost;
    		int Points;
     
    		public void setteam(String team) //defining team names as string
            {
            this.team = team; //Sets team
            }
     
            public String toString() {
            	StringBuilder organiselayout = new StringBuilder(); 
                organiselayout.append(this.team);
     
                return organiselayout.toString();
            }
        }
     
     
     
    	generatortest() throws IOException {
     
     
    		BufferedReader bufferedReader = new BufferedReader(new FileReader("PL.txt")); //Reads In Assignment file or your file.
     
    		if (bufferedReader != null) {
     
                String text; //sets each line read as a text string
     
                while ((text = bufferedReader.readLine()) != null) {
                     String[] split = text.split(":"); // splits the string text at each colon
     
        	         List<Team> Games = new ArrayList<Team>(); //Listing the array of the Team class
     
    // If the Length of string is greater than 4 then match is classed as valid
                    if (split.length >= 4) { 
                        Team League = new Team(); //adds the text values to the Team Class
                        League.setteam(split[0].trim()); //the 0 string is the home team when reading from the file
                        League.setteam(split[1].trim()); //the 1 string is the away team when reading from the file
     
                        Games.add(League); //adds the previous games into 1 entity
                		System.out.println(League.toString());
                    }   
                }
     
     
                bufferedReader.close(); //closes reader
     
            }
     
    		}
     
     
    	public final static void main(String [] args) throws IOException {
     
    		new generatortest();
     
     
    	}
     
    }

    I know it contains integers that aren't used locally at the minute but i eventually want to compare this list and re-loop through the text file taking more information to build myself a league table..

    Thanks RSYR


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: array/string problem

    Override the equals and hashCode methods in your Team object, using the team name to check for equality. From there you can check your list using the contains() method specified in List to see if it contains a team. If it doesn't add the new Team, if it does you should create a method in Team to combine a previous team in the list with the new data you've collected.

Similar Threads

  1. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  2. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM
  3. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM