1

Arrays:

static String[] names;
static int[] points;
static int[] goals_scored;
static int[] goles_received;

I am asked in an exercise to get the second highest number in points array, then display the second highest team by it's name, then the points, the goals, etc like this e.g.:

Name: FC Spain
Points: 50
Goals scored: 43
Goals received: 27

I though about sorting the array but the problem is that if I sort the points array, then, the second highest position of points would not be the same as the names/goals array, also i can not sort also the goals arrays because the best team (e.g.) won't have the max goals. Right?

But also in another execise I have to display the data of the teams according to a logical criterion (like in ascending order) so I do not know what to do.

EDIT: Also, the user enters the max values that the arrays can have.

6
  • 4
    Would a single Team[] not make more sense here? Commented Nov 19, 2017 at 16:52
  • With the multiple names, points and goals inside a single array? Commented Nov 19, 2017 at 16:53
  • Each name, points and goals inside a single class, and then an array of that class. Commented Nov 19, 2017 at 16:54
  • How? Sorry, I have not reached classes yet. Commented Nov 19, 2017 at 16:57
  • 2
    Then reach them. They're explained in any Java introductory book, and in the Java tutorial. Doing it with arrays would be much more complex and awkward. Commented Nov 19, 2017 at 17:00

1 Answer 1

1

You should create a class that has those fields(Name, Points, Goals scored and Goals recieved). Then you should create an array of that class. And just create those teams. And sort them out by it's goals... So the class should look something like this:

public class Team{
    private String name;
    private int points;
    private int goalsScored;
    private int goalsRecieved;


    public Team(String teamName, int p, int gs, int gr){
        this.name = teamName;
        this.points = p;
        this.goalsScored = gs;
        this.goalsRecieved = gr;
    }


   public int getPoints(){
       return this.points;
   }

   public void setPoints(int points){
       this.points = points;
   }

   public void setName(String name){
       this.name = name;
   }
}

And then you should create the array of that class somewhere and sort it by points:

public class Main{
     public static void main(String[] args){
            Team[] teams = new Team[numberOfTeams];
            //initialize every team in the array like this:
            teams[0] = new Team("Spain", 50, 43, 27);
            //and so on for every team
            //and then sort them out by teams[index].getPoints();
     }
}
Sign up to request clarification or add additional context in comments.

7 Comments

And how do i do to acces the arrays from other methods and print them like with ArraytoString format?
Hmm I didn't fully understand what do you mean?! What I understood you ask how to access the array in some method? All you have to do is declare that Array in the class, I declared it in the main method...
Btw I would suggest you Head first Java book, it's beginner friendly and it would teach you a lot of OOP things
Yeah but also access the variables like name, points, etc so the user can enter the names, points, etc for each team.
Just create methods in the Team class for that like: public void setName(String name){ this.name = name; } public void setPoints(int points){ this.points = points } and so on..... I'll edit the answer above so you can see it clearly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.