3 Replies - 260 Views - Last Post: 19 June 2013 - 10:39 PM Rate Topic: -----

#1 savage24x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-June 13

ArrayList clear function problem.

Posted 19 June 2013 - 07:51 PM

When I run my newly created program, it completely clears two arraylists, then doesn't do the functions after I clear them. This is the only thing that is stopping my program from working correctly, can anybody take a look?

package Final;

import java.util.*;

public class Schedule {
	public String teams;
	public Scanner s = new Scanner(System.in);
	public List<String> teamset1;
	public List<String> teamset2;
	public List<String> teamset3;
	public List<String> teamset4;
	public List<String> teamset5;
	public List<String> teamset6;
	public List<String> qf1 = new ArrayList();
	public List<String> qf2 = new ArrayList();
	public List<String> sf1 = new ArrayList();
	public List<String> sf2 = new ArrayList();
	public List<String> f1 = new ArrayList();
	public List<String> f2 = new ArrayList();
	public List<String> match1 = new ArrayList();
	public List<String> match2 = new ArrayList();
	public List<String> winner = new ArrayList();
	public Random r = new Random();

	public static void main(String[] args) {
		Schedule test = new Schedule();
		test.go();
	}
	public void go(){
		System.out.println("Please enter in 16 teams, separated by commas. You can use any type of format for the team names.");
		teams = s.nextLine();
		Team awesome = new Team();
		awesome.convert(teams);
		teamset1 = awesome.set1;
		teamset2 = awesome.set2;
		teamvs(teamset1);
		teamvs(teamset2);
		System.out.println("Here are your matchups!\n");
		System.out.println(match1.toString());
		System.out.println(match2.toString());
		System.out.println("\nLet's go to the first matches, shall we?");
		findQF(teamset1);
		findQF(teamset2);
		System.out.println("Here are your first match winners!\n");
		System.out.println(qf1.toString());
		System.out.println(qf2.toString());
		System.out.println("\nLooks like we have some great matches to look forward to! Here's the quarter finals!\n");
		teamvs(qf1);
		teamvs(qf2);
		System.out.println("Ready for the next matchups? Semi Final time!");
		System.out.println(match1.toString());
		System.out.println(match2.toString());
		System.out.println("Great games! Let's figure out the winners!");
		findSF(qf1);
		findSF(qf2);
		System.out.println("And here come the semi final winners!");
		System.out.println(sf1.toString());
		System.out.println(sf2.toString());
		System.out.println("The new matchups are coming up next.");
		teamvs(sf1);
		teamvs(sf2);
		System.out.println(match1.toString());
		System.out.println(match2.toString());
		System.out.println("One more match. Here are the winners of the Finals. This next match is for the championship!");
		findF(sf1);
		findF(sf2);
		System.out.println(f1.toString());
		System.out.println(f2.toString());
		System.out.println("Here's the championship matchup!");
		teamvs(f1);
		teamvs(f2);
		System.out.println("Ladies and gentlemen, your winner is: " + winner.get(0) + "!");
	}
	public void teamvs(List<String> s){
		match1.clear();
		match2.clear();
		if (s == teamset1 || s == qf1){
			for(int i=0; i<s.size()-1; i+=2){
				match1.add(s.get(i) + " vs. " + s.get(i+1));
			}
		} else if (s == teamset2 || s == qf2){
			for(int i=0; i<s.size()-1;i+=2){
				match2.add(s.get(i) + " vs. " + s.get(i+1));
			}
		}
	}
	public void findQF(List<String> s){
		List<String> temp = new ArrayList();
		if (s == teamset1 || s==teamset3){
			for(int i=0; i<s.size()-1; i+=2){
				temp.add(0, s.get(i));
				temp.add(1, s.get(i+1));
				int x = r.nextInt(2);
				if(x == 0){
					qf1.add(temp.get(0));
				} else {
					qf1.add(temp.get(1));
				}
			}
		} else if (s == teamset2 || s==teamset4){
			for(int j=0; j<s.size()-1; j+=2){
				temp.add(0, s.get(j));
				temp.add(1, s.get(j+1));
				int x = r.nextInt(2);
				if(x == 0){
					qf2.add(temp.get(0));
				} else {
					qf2.add(temp.get(1));
				}
			}
		}
	}

	public void findSF(List<String> s){
		List<String> temp = new ArrayList();
		if (s == teamset3){
			for(int i=0; i<s.size()-1; i+=2){
				temp.add(0, s.get(i));
				temp.add(1, s.get(i+1));
				int x = r.nextInt(2);
				if(x == 0){
					sf1.add(temp.get(0));
				} else {
					sf1.add(temp.get(1));
				}
			} 
		} else if (s == teamset4){
			for(int j=0; j<s.size()-1; j+=2){
				temp.add(0, s.get(j));
				temp.add(1, s.get(j+1));
				int x = r.nextInt(2);
				if(x == 0){
					sf2.add(temp.get(0));
				} else {
					sf2.add(temp.get(1));
				}
			}
		}
	}
	
	public void findF(List<String> s){
		List<String> temp = new ArrayList();
		if (s == teamset5){
			for(int i=0; i<s.size()-1; i+=2){
				temp.add(0, s.get(i));
				temp.add(1, s.get(i+1));
				int x = r.nextInt(2);
				if(x == 0){
					f1.add(temp.get(0));
				} else {
					f1.add(temp.get(1));
				}
			} 
		} else if (s == teamset6){
			for(int j=0; j<s.size()-1; j+=2){
				temp.add(0, s.get(j));
				temp.add(1, s.get(j+1));
				int x = r.nextInt(2);
				if(x == 0){
					f2.add(temp.get(0));
				} else {
					f2.add(temp.get(1));
				}
			}
		}
	}
	
	public void winner(){
		List<String> temp = new ArrayList();
		temp.add(0, f1.get(0));
		temp.add(1, f2.get(0));int x = r.nextInt(2);
		if(x == 0){
			winner.add(temp.get(0));
		} else {
			winner.add(temp.get(1));
		}
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList clear function problem.

#2 savage24x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-June 13

Re: ArrayList clear function problem.

Posted 19 June 2013 - 08:01 PM

Well guys, I'm an idiot. Just some syntax errors. Took me about 10 minutes to get it all sorted out.
Was This Post Helpful? 0
  • +
  • -

#3 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: ArrayList clear function problem.

Posted 19 June 2013 - 08:06 PM

Human error is not a measure of intellectual aptitude. But you might want to name your variables to something more readable for others if you need help in the future.

Also, when calling System.out.println(string), an object's toString() method is called automatically. You don't need to explicitly call it, save yourself some repetition. :)
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 138
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: ArrayList clear function problem.

Posted 19 June 2013 - 10:39 PM

also, why not create a constructor instead of a public void go()

It'll save you a line in main :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1