I'm need to set up teams based on how many wins the team has inside this text file :

UNCC 30
NCSU 28
GT 25
USC 20
GSU 19
UVA 15
UGA 2
UNC 1

For example the output of the program should be:

UNCC VS UNC
NCSU VS UGA
GT VS UVA
USC VS GSU

This is my code:

import java.io.*;
import java.util.Scanner;


public class Tournmanet
{
public static void main(String [] args) throws IOException
{
File file = new File("Teams.txt");
Scanner input = new Scanner(file);

String w;
int wins;
String teamName;
int gameCounter=0;
Teams [] game = new Teams [8];

 for (int i= 0; i < game.length; i++)
 {
  teamName = input.next();
  w= input.next();
  wins= Integer.parseInt(w);
  game[i] = new Teams(teamName, wins);
 }

	for (int i = 0; i < game.length-1; i+=2)
	{
	for(int j= 0; j< game.length; j+=2)
	{
		if(i > game.length)
		j--;
		System.out.println(game[i+1].getName()+ " VS." + game[j].getName());
	}
        }
}
}

class Teams
{
String teamName;
int wins;

public Teams()
{
}

public Teams (String teamName, int wins)
{
this.teamName = teamName;
this.wins = wins;
}

public String getName()
{
return teamName;
}
public int getWins()
{
return wins;
}
}

THIS IS THE OUTPUT I GET:

NCSU VS.UNCC
NCSU VS.GT
NCSU VS.GSU
NCSU VS.UGA
USC VS.UNCC
USC VS.GT
USC VS.GSU
USC VS.UGA
UVA VS.UNCC
UVA VS.GT
UVA VS.GSU
UVA VS.UGA
UNC VS.UNCC
UNC VS.GT
UNC VS.GSU
UNC VS.UGA

Any ideas? Thanks!

You only need a single loop through games[] to pair the teams. You just step in from the ends evenly as you loop, so first pair is 0 and length-1. Think about the loop you need to select the rest based on that progression to the middle.

I now get this with just one loop.

NCSU VS.UNCC
GT VS.NCSU
USC VS.GT

This is my loop now btw:

for(int j= 0; j< game.length-1; j++)
	{
		if(j > game.length)
		j++;
		System.out.println(game[j+1].getName()+ " VS." + game[j].getName());
	}

Write down the index values for the first and the last. That is your first pairing. Then write the next pair of indices. Note the values that need to be incremented each time as you loop to the middle. It is not j and j+1.

Sorry I'm a little lost about what you just said. This is my first class in java.

You need to select the first and last elements from the array: [0] vs [length-1] The second pairing is the second element and the next to last: [1] vs [length-2] This repeats until you reach the middle of the array.

All you have to do is create the loop for that progression. It is a single loop that terminates halfway through the data set. Your loop index is all you need to select each pair of values because they step in at the same rate.

So I got it to display all the teams like:
UNCC VS.UNCC
NCSU VS.NCSU
GT VS.GT
USC VS.USC

But I'm still not understand what I need to do so it creates the teams based on the amount of wins. Thanks a lot for your help as this is due at 9:00 pm tonight.

This is what I have now

import java.io.*;
import java.util.Scanner;


public class Tournmanet
{
public static void main(String [] args) throws IOException
{
File file = new File("Teams.txt");
Scanner input = new Scanner(file);

String w;
int wins;
String teamName;
int gameCounter=0;
Teams [] game = new Teams [4];

 for (int i= 0; i < game.length; i++)
 {
  teamName = input.next();
  w= input.next();
  wins= Integer.parseInt(w);
  game[i] = new Teams(teamName, wins);
 }


	for(int i= 0; i< game.length; i++)
	{
		if(i > game.length)
		i++;;
		System.out.println(game[i].getName()+ " VS." + game[i].getName());
	}
        
}
}

class Teams
{
String teamName;
int wins;

public Teams()
{
}

public Teams (String teamName, int wins)
{
this.teamName = teamName;
this.wins = wins;
}

public String getName()
{
return teamName;
}
public int getWins()
{
return wins;
}
}

What I need to understand is how to write the code so it will find the lowest and highest value in an array but somehow so it will continue to loop through until it reaches the end of the array or something?

The example you gave above was already sorted from most to least wins. Pairing the teams is one simple loop to the middle of the array, selecting the "i"th element from either end of the array. I wrote out those array indexes in my last post. If you produced
UNCC VS.UNCC
NCSU VS.NCSU
then you obviously did not grab the elements that I described.

When i do change these two lines:

Teams[] game = new Teams[7];

System.out.println(game[j].getName()+ " VS." + game[j+6].getName());

I get what your saying now but I now get this error

"ArrayIndexOutOfBoundsException: 7"

j+6 is not what I wrote above. That is quite different than length-1.

It won't let me do length-1... compiler error:

symbol : variable length
location: class Tournmanet
System.out.println(game[j].getName()+ " VS." + game[length-1].getName());

I was referring to your array.length. The last element of game[] is game[game.length-1] .

Is this what your talking about.

System.out.println(game[j].getName()+ " VS." + game[game.length-1].getName());

I'm sorry i'm just not understanding what your saying. I know you must be frustrated but thank you so much for your help!

Yes, that is nearly what I am referring to, however you still need to incorporate your loop index "j" into that second expression involving length.

Think about what element you need for the second pair and how it relates to the array length.

I get what your saying and I'm looking through my book but I don't see how I separate the length-1 and incorporate my index into the statement so it's an legal expression. I tried separating with a "," and another "[]" but nothing seems to work.

The last element is game.length-1 .
The second to last is game.length-2 .
... see where this is going? How can you use the value of j to get those elements?

I actually don't get what this is doing... my output with what I posted above is:
UNCC VS.UGA
GT VS.UGA
GSU VS.UGA
UGA VS.UGA

It's using UGA for the last part of every statement and skipping NCSU,USC,UVA, and UNC?

I see my expression j++ is causing it to skip those teams... why?

Ok, more explicitly:
[game.length-1-0] = "UNC"
[game.length-1-1] = "UGA"
[game.length-1-2] = "UVA" game.length-1 itself is just an expression for "the last element of my array". It's fixed and you are walking toward the middle from that value. What part of the above progression relates to your loop variable?

Now i'm getting this....
UNCC VS.USC
NCSU VS.USC
GT VS.USC
USC VS.USC

Well, you haven't posted your loop code and I can't see over your shoulder.

I actually have to leave for the day, so I will just leave you with the advice to study how the elements in my previous post relate to the loop index and I will reiterate that you only need to loop through half of the array.

for(int j = 0; j< game.length;j++)
	{
		System.out.println(game[j].getName()+ " VS." + game[game.length-1-j].getName());	
		
	}

I'm not sure where you ended up with this or if you are still working on it, but the loop you posted runs the entire array length and that is more than necessary.

I've already turned in the assignment because it was due. I know it wasn't done right but I didn't want a complete zero... Still wish I could figure this out though for my knowledge.

game.length/2 is half the array. Did you ever try that with your loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.