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

Thread: Can't Find Problem

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Can't Find Problem

    Ok, so I am building a program that will fix my Fantasy Football Schedules so that every team plays each other at least once, and no more than twice, and evens up the Home/Away orders.

    Right now, I am just trying to read the schedule from a text file and set up all the arrays of Teams and Matchups. The problem with the code is that it doesnt seem to set up the Seasons correctly. To be exact, some teams seem to be missing some games, and almost every team seems to have incorrect Home/Away Counts. All teams will have 13 games.

    It is alot of code, but I think it is organized.

    Main Class:
    public class FantasyFootballScheduler 
    {
     
        public static void main(String[] args) 
        {
    		new Program();
        }
    }

    Program Class:
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class Program 
    {
    	ArrayList<Team> teamList = new ArrayList<Team>();
     
        public Program() 
        {
        	try{
    	    	Scanner in = new Scanner(new File("Teams.txt"));
     
    	    	String line = in.nextLine();
     
    	    	line = in.nextLine();
    	    	if(line.indexOf("AWAY TEAM")!=-1)
    	    		line = in.nextLine();
     
    	    	boolean week1 = true;
     
    	    	while(in.hasNextLine())
    	    	{
    	    		if(line.equals(" "))
    	    		{	
    	    			line = in.nextLine();
    	    			line = in.nextLine();
    	    			line = in.nextLine();
    	    			week1=false;
    	    		}
    	    		int indexLast = line.indexOf("(");
    	    		int indexF = line.indexOf("at");
    	    		int indexL = line.lastIndexOf("(");
    	    		String home = line.substring(0,indexLast-1).trim();
    	    		String away = line.substring(indexF+2,indexL-1).trim();	    		
     
    	    		if(week1)
    	    		{
    	    			Team h = new Team(home);
    		    		h.addMatchup(away,true);
    		    		Team a = new Team(away);
    		    		a.addMatchup(home,false);
    		    		teamList.add(h);
    	    			teamList.add(a);
    	    		}
    	    		else
    	    		{
    	    			for(int i=0;i<teamList.size();i++)
    	    			{
    	    				if(teamList.get(i).isTeam(home))
    	    				{
    	    					Team t = teamList.get(i);
    	    					t.addMatchup(away,true);
    	    				}
    	    				if(teamList.get(i).isTeam(away))
    	    				{
    	    					Team t = teamList.get(i);
    	    					t.addMatchup(home,false);
    	    				}
    	    			}
    	    		}
     
    	    		line = in.nextLine();
    	    	}
     
        	}catch(Exception ex){System.out.println(ex);   	}
     
        	System.out.println(teamList.size());
        	/*for(int i=0;i<teamList.size();i++)
        	{
        		teamList.get(i).setCounter(teamList);
        		teamList.get(i).printCounter();
        	}*/
     
        	for(int x=0;x<teamList.size();x++)
    	    {
    	    	Team t = teamList.get(x);
    	    	System.out.println(t.name);
    	    	/*for(int i=0;i<t.season.size();i++)
    	    	{
    	    		System.out.println(t.season.get(i));
    	    	}*/
    	    	System.out.println("Home Games: "+t.Homes);
    	    	System.out.println("Away Games: "+t.Aways);
    	    	System.out.println();
        	}
        }
    }

    Team Class:
    import java.util.ArrayList;
     
     
    public class Team 
    {
    	String name;
    	ArrayList<Matchup> season;
    	ArrayList<Counter> counter;
    	int Homes;
    	int Aways;
     
        public Team(String n) 
        {
        	name = n;
        	season = new ArrayList<Matchup>();
        	Homes = 0;
        	Aways = 0;
        }
     
        public void addMatchup(Matchup m)
        {
        	season.add(m);
        	addDirection();
        }
     
        public void addMatchup(String n,boolean home)
        {
        	if(home)
        		addMatchup(new Matchup(name,n));
        	else
        		addMatchup(new Matchup(n,name));
        }
     
        public void addDirection()
        {
        	Matchup mat = season.get(season.size()-1);
        	if(mat.home.equals(name))
        		Homes++;
        	else if(mat.away.equals(name))
        		Aways++;
        }
     
        public boolean isTeam(String s)
        {
        	if(s.equalsIgnoreCase(name))
        		return true;
        	return false;
        }
     
        public void setCounter(ArrayList<Team> arr)
        {
        	counter = new ArrayList<Counter>();
        	for(int i=0;i<arr.size();i++)
        	{
        		counter.add(new Counter(arr.get(i)));
        	}
        	parseSeason();
        }
     
        public void parseSeason()
        {
        	for(int i=0;i<season.size();i++)
        	{
        		for(int x=0;x<counter.size();x++)
        		{
        			if(counter.get(x).isCounter(season.get(i).getOpponent(name)))
        			{
        				counter.get(x).add();
        				break;
        			}
        		}
        	}
        }
     
        public void printCounter()
        {
        	System.out.println(name);
        	for(int i=0;i<counter.size();i++)
        	{
        		System.out.println(counter.get(i));
        	}
        	System.out.println();
        }
     
    }

    Matchup Class:
    public class Matchup 
    {
    	String home;
    	String away;
     
        public Matchup(String h,String a) 
        {
        	home = h;
        	away = a;
        }
     
        public String getOpponent(String n)
        {
        	if(home.equalsIgnoreCase(n))
        		return away;
        	return home;
        }
     
        public String toString()
        {
        	return away+" at "+home;
        }
     
    }

    Counter Class:
    public class Counter 
    {
    	Team team;
    	int count;
     
        public Counter(Team t) 
        {
        	team = t;
        	count = 0;
        }
        public void add()
        {
        	count++;
        }
     
        public boolean isCounter(String n)
        {
        	return (team.isTeam(n));	
        }
     
        public String toString()
        {
        	return team.name+": "+count;
        }
     
    }


    Input File:
    WEEK 1 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Pittsburgh Steelers (0-0) ad mc at Big Hitter The Lama (0-0) Joel Schofield
    FIVE VOWS (0-0) john mclaughlin at New Orleans Saints (0-0) James Stanford
    MMMMMM Football (0-0) Travis owens at Jokers Lunatics (0-0) Mike Ingersoll
    Sanford and Son (0-0) Devin James at Albert Gallatin Colonials (0-0) Kevin Rodgers
    Sacto Giant Horridas (0-0) Steven Spalla at Erial Eggheads (0-0)

    WEEK 2 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    FIVE VOWS (0-0) john mclaughlin at Pittsburgh Steelers (0-0) ad mc
    Big Hitter The Lama (0-0) Joel Schofield at Sacto Giant Horridas (0-0) Steven Spalla
    New Orleans Saints (0-0) James Stanford at Jokers Lunatics (0-0) Mike Ingersoll
    Erial Eggheads (0-0) at Sanford and Son (0-0) Devin James
    MMMMMM Football (0-0) Travis owens at Albert Gallatin Colonials (0-0) Kevin Rodgers

    WEEK 3 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    MMMMMM Football (0-0) Travis owens at Sacto Giant Horridas (0-0) Steven Spalla
    FIVE VOWS (0-0) john mclaughlin at Jokers Lunatics (0-0) Mike Ingersoll
    New Orleans Saints (0-0) James Stanford at Big Hitter The Lama (0-0) Joel Schofield
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Erial Eggheads (0-0)
    Pittsburgh Steelers (0-0) ad mc at Sanford and Son (0-0) Devin James

    WEEK 4 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    MMMMMM Football (0-0) Travis owens at Pittsburgh Steelers (0-0) ad mc
    FIVE VOWS (0-0) john mclaughlin at Sanford and Son (0-0) Devin James
    New Orleans Saints (0-0) James Stanford at Albert Gallatin Colonials (0-0) Kevin Rodgers
    Big Hitter The Lama (0-0) Joel Schofield at Erial Eggheads (0-0)
    Sacto Giant Horridas (0-0) Steven Spalla at Jokers Lunatics (0-0) Mike Ingersoll

    WEEK 5 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Pittsburgh Steelers (0-0) ad mc at Sacto Giant Horridas (0-0) Steven Spalla
    Sanford and Son (0-0) Devin James at New Orleans Saints (0-0) James Stanford
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Big Hitter The Lama (0-0) Joel Schofield
    Erial Eggheads (0-0) at Jokers Lunatics (0-0) Mike Ingersoll
    MMMMMM Football (0-0) Travis owens at FIVE VOWS (0-0) john mclaughlin

    WEEK 6 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Sacto Giant Horridas (0-0) Steven Spalla at MMMMMM Football (0-0) Travis owens
    Big Hitter The Lama (0-0) Joel Schofield at Sanford and Son (0-0) Devin James
    Jokers Lunatics (0-0) Mike Ingersoll at Albert Gallatin Colonials (0-0) Kevin Rodgers
    FIVE VOWS (0-0) john mclaughlin at Erial Eggheads (0-0)
    Pittsburgh Steelers (0-0) ad mc at New Orleans Saints (0-0) James Stanford

    WEEK 7 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Pittsburgh Steelers (0-0) ad mc at Sacto Giant Horridas (0-0) Steven Spalla
    Sanford and Son (0-0) Devin James at Jokers Lunatics (0-0) Mike Ingersoll
    Albert Gallatin Colonials (0-0) Kevin Rodgers at FIVE VOWS (0-0) john mclaughlin
    Erial Eggheads (0-0) at New Orleans Saints (0-0) James Stanford
    MMMMMM Football (0-0) Travis owens at Big Hitter The Lama (0-0) Joel Schofield

    WEEK 8 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    New Orleans Saints (0-0) James Stanford at FIVE VOWS (0-0) john mclaughlin
    Jokers Lunatics (0-0) Mike Ingersoll at Big Hitter The Lama (0-0) Joel Schofield
    Pittsburgh Steelers (0-0) ad mc at Sanford and Son (0-0) Devin James
    MMMMMM Football (0-0) Travis owens at Albert Gallatin Colonials (0-0) Kevin Rodgers
    Sacto Giant Horridas (0-0) Steven Spalla at Erial Eggheads (0-0)

    WEEK 9 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    FIVE VOWS (0-0) john mclaughlin at Big Hitter The Lama (0-0) Joel Schofield
    Jokers Lunatics (0-0) Mike Ingersoll at New Orleans Saints (0-0) James Stanford
    Sanford and Son (0-0) Devin James at MMMMMM Football (0-0) Travis owens
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Sacto Giant Horridas (0-0) Steven Spalla
    Erial Eggheads (0-0) at Pittsburgh Steelers (0-0) ad mc

    WEEK 10 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Jokers Lunatics (0-0) Mike Ingersoll at FIVE VOWS (0-0) john mclaughlin
    Big Hitter The Lama (0-0) Joel Schofield at New Orleans Saints (0-0) James Stanford
    Sacto Giant Horridas (0-0) Steven Spalla at Sanford and Son (0-0) Devin James
    Pittsburgh Steelers (0-0) ad mc at MMMMMM Football (0-0) Travis owens
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Erial Eggheads (0-0)

    WEEK 11 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    FIVE VOWS (0-0) john mclaughlin at Sacto Giant Horridas (0-0) Steven Spalla
    Big Hitter The Lama (0-0) Joel Schofield at Jokers Lunatics (0-0) Mike Ingersoll
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Pittsburgh Steelers (0-0) ad mc
    Erial Eggheads (0-0) at MMMMMM Football (0-0) Travis owens
    Sanford and Son (0-0) Devin James at New Orleans Saints (0-0) James Stanford

    WEEK 12 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Albert Gallatin Colonials (0-0) Kevin Rodgers at Jokers Lunatics (0-0) Mike Ingersoll
    Pittsburgh Steelers (0-0) ad mc at FIVE VOWS (0-0) john mclaughlin
    MMMMMM Football (0-0) Travis owens at New Orleans Saints (0-0) James Stanford
    Sacto Giant Horridas (0-0) Steven Spalla at Big Hitter The Lama (0-0) Joel Schofield
    Erial Eggheads (0-0) at Sanford and Son (0-0) Devin James

    WEEK 13 (Edit)
    AWAY TEAM OWNER(S) HOME TEAM OWNER(S) RESULT
    Sanford and Son (0-0) Devin James at Albert Gallatin Colonials (0-0) Kevin Rodgers
    FIVE VOWS (0-0) john mclaughlin at MMMMMM Football (0-0) Travis owens
    New Orleans Saints (0-0) James Stanford at Sacto Giant Horridas (0-0) Steven Spalla
    Jokers Lunatics (0-0) Mike Ingersoll at Pittsburgh Steelers (0-0) ad mc
    Erial Eggheads (0-0) at Big Hitter The Lama (0-0) Joel Schofield

    Current Output:
    Pittsburgh Steelers
    Home Games: 8
    Away Games: 3

    Big Hitter The Lama
    Home Games: 5
    Away Games: 5

    FIVE VOWS
    Home Games: 8
    Away Games: 3

    New Orleans Saints
    Home Games: 5
    Away Games: 7

    MMMMMM Football
    Home Games: 8
    Away Games: 5

    Jokers Lunatics
    Home Games: 5
    Away Games: 7

    Sanford and Son
    Home Games: 6
    Away Games: 7

    Albert Gallatin Colonials
    Home Games: 7
    Away Games: 5

    Sacto Giant Horridas
    Home Games: 6
    Away Games: 6

    Erial Eggheads
    Home Games: 6
    Away Games: 4
    Desired Output:
    Pittsburgh Steelers
    Home Games: 5
    Away Games: 8

    Big Hitter The Lama
    Home Games: 8
    Away Games: 5

    FIVE VOWS
    Home Games: 5
    Away Games: 8

    New Orleans Saints
    Home Games: 8
    Away Games: 5

    MMMMMM Football
    Home Games: 5
    Away Games: 8

    Jokers Lunatics
    Home Games: 8
    Away Games: 5

    Sanford and Son
    Home Games: 7
    Away Games: 6

    Albert Gallatin Colonials
    Home Games: 6
    Away Games: 7

    Sacto Giant Horridas
    Home Games: 7
    Away Games: 6

    Erial Eggheads
    Home Games: 6
    Away Games: 7
    So I think that is all the information I can give regarding my issue, since I have given everything.
    Last edited by aussiemcgr; August 14th, 2010 at 05:02 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Find Problem

    some teams seem to be missing some games,
    Can you be a lot more specific.
    What teams and what games? Is it the same teams every time?
    How is the data input to the program? Where can the counts be missed?

    You could annotate the output to show which values are wrong to save anyone having to scroll up and down and up and down and up and down to find a mismatch. A 2 columned posting of current and desired would allow looking at both at the same time.

    How are you approaching the debugging of your program? Adding println()s to show all the counts as they are computed?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't Find Problem

    There also seems to be the issue that the program treats Home games as Away games and Away games as Home games. I dont think I implemented it incorrectly though...

    Quote Originally Posted by Norm View Post
    Can you be a lot more specific.
    What teams and what games? Is it the same teams every time?
    I actually found some interesting issues here. The data doesnt seem to be getting read correctly or the Strings arent getting separated correctly for some of the Matches.

    Here is a list of the missing games:
    Pittsburgh Steelers
    Week 12 and 13

    MMMMMM Football
    NONE

    Sacto Giant Horridas
    Week 9

    FIVE VOWS
    Week 7 and 10


    I can continue on, but I'm noticing a HUGE issue with the data input that is weird as hell, and fixing this first might lead to the solution.

    How is the data input to the program?
    The data should be inputted into the program via Scanner. As can be seen by the statement:
    Scanner in = new Scanner(new File("Teams.txt"));
    String line = in.nextLine();
    But I have been having issues with this (and every other form of text reading I've tried here).
    After reading in the line, I have attempted to separate the Home team from the Away team. To do this, I have tried to use these statements:
    int indexLast = line.indexOf("(");
    int indexF = line.indexOf("at");
    int indexL = line.lastIndexOf("(");
    String home = line.substring(0,indexLast-1).trim();
    String away = line.substring(indexF+2,indexL-1).trim();
    I then realized (while compiling the list above for you) that one of the team names contains the word "at" in it, which could be core of many of my issues. So I changed int indexF = line.indexOf("at"); to int indexF = line.indexOf(" at ");, which also meant I had to change String away = line.substring(indexF+2,indexL-1).trim();. However, when I did that, it resulted in the first two letters of every HOME team to get cut off. That just doesnt even make sense.

    Now, the input file has an interesting trait to it which cannot be seen thanks to the auto-aligning or whatever it is on this forum. The input file contains tabs between different parts that I think may be causing my " at " issue. I believe there is a tab before the word "at". How can I separate the String based on tabbing before the word at?

    For the time being, I am using int indexF = line.indexOf("at ");, which seems to work as there is 3 spaces before the next tab.

    The last problem with the input is the statement: in.hasNextLine() in the while loop. It doesnt seem to want to read the last line unless I put an extra line under it with some character (like a period or something). How can I get it to read the last line in the loop?

    So, while typing up this, I have solved the largest of my issues, but in a rather messy way. My only significant issue right now is that I still don't know why the Home and Away games are swapped.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Find Problem

    ow can I separate the String based on tabbing before the word at?
    Regular expressions treat tabs as white spaces.


    Problem with some of the Scanner methods is they can block. I think even hasNext can block (have to check)
    Better for your input would be a normal file reader that returns null at EOF.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't Find Problem

    Quote Originally Posted by Norm View Post
    Regular expressions treat tabs as white spaces.


    Problem with some of the Scanner methods is they can block. I think even hasNext can block (have to check)
    Better for your input would be a normal file reader that returns null at EOF.
    I tried using a FileReader with a BufferedReader, but I couldnt get the BufferedReader.readLine() to work every time. I have no idea what it was doing, but it wasnt reading with any consistency. Sometimes it threw null pointers at random, sometimes it didnt move to the next line, and all sorts of issues. I ran the same code and same input 3 times, each immediately after another, and it gave 3 completely different outputs. I couldnt explain it and got really frustrated, so I moved on to Scanner.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Find Problem

    I couldnt get the BufferedReader.readLine() to work every time
    I think the problem was your logic.
    If you run the same program with the same input, it should work the same every time.

    How can I get it to read the last line in the loop?
    Your program does read the last line but it doesn't process it.
    What happens with this loop:
                while(in.hasNextLine())             {
                       ....
     
                    line = in.nextLine();    // If you read the last line here, is there a NextLine? if not, you exit the loop
                } // end while()

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't Find Problem

    Ok, so if I make the transition at the start of the loop (and adjust the moves before the loop accordingly), then I could read the last line.

    I think the problem was your logic.
    If you run the same program with the same input, it should work the same every time.
    Believe me man, I know that. I promise you, I change NOTHING between the executions and I was getting several different answers. The most infuriating thing for me was that only sometimes the BufferedReader.readLine() would actually move onto the next line. I was outputting the line, calling the BufferedReader.readLine() method, outputting the line again, and it would sometimes give me the same result, and sometimes move the line. I couldnt explain it and the more I kept running the data, the less it was making sense, so I just moved on. All I know is that Scanner is moving the line like it should. In fact, the code I have now is the same (just swapped the .readLine() for .nextLine()) as what I had with BufferedReader.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Find Problem

    Can you make a version of your program that has these random errors and post it here?

Similar Threads

  1. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  2. where can i find the tmp/foo directory?
    By Idy in forum Java IDEs
    Replies: 11
    Last Post: January 19th, 2010, 11:21 AM
  3. cannot find symbol - method
    By kyuss in forum Object Oriented Programming
    Replies: 2
    Last Post: December 7th, 2009, 01:01 PM
  4. SIGAR to find CPU info-problem
    By ttsdinesh in forum Exceptions
    Replies: 7
    Last Post: October 4th, 2009, 10:33 AM
  5. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM