Arrays.sort() only sorts the array you tell it to sort - it doesn't know anything about any parallel arrays. If you want to sort two parallel arrays you will have to write your own sort function, but the better way to do this is to create a class that contains team name and year fields and create an array of instances of this class and then sort that.

Sujath Mohammed wrote:I am using arrays because the assignment says i have to...

In which case you will need some sort of custom sort; but first you'll need to extract distinct team names from your winners list. One way to do that is to convert it to a Set (java.util.Set) first, viz:(I'll leave you to work out the mechanics)

If you sort that, you'll then have an alphabetical list of winning teams which you can then use to run through your parallel arrays.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 03:35:16

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:I am using arrays because the assignment says i have to...

In which case you will need some sort of custom sort; but first you'll need to extract distinct team names from your winners list. One way to do that is to convert it to a Set (java.util.Set) first, viz:(I'll leave to to work out the mechanics)

If you sort that, you'll then have an alphabetical list of winning teams which you can then use to run through your parallel arrays.

Winston

I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please
thank you

Sujath Mohammed wrote:I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please

OK.

You say you want to sort your teams alphabetically; but from what I see, you don't want to sort ALL your winners, you want to sort an array that contains each winning team exactly once (that's what 'distinct' means). Sets (and HashSet is a Set) guarantee this as part of their contract.

Alternatively, you can extract distinct team names yourself - it's not too difficult - but its an array of distinct team names that you need to sort, not all your winners. Indeed, sorting a copy of your winners array may be the easiest way to start.

The fact is that there's more than one way to do this, but what you mustn't do is sort the winners array itself; otherwise you'll screw up all that lovely matching between your two arrays.

Question: were you specifically told to use TWO arrays as you've done, or were you simply told that you must use arrays?

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 03:55:09

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please

OK.

You say you want to sort your teams alphabetically; but from what I see, you don't want to sort ALL your winners, you want to sort an array that contains each winning team exactly once (that's what 'distinct' means). Sets (and HashSet is a Set) guarantee this as part of their contract.

Alternatively, you can extract distinct team names yourself - it's not too difficult - but the fact is that its an array of distinct team names that you need to sort, not all your winners. Indeed, sorting a copy of your winners array may be the easiest way to start.

The fact is that there's more than one way to do this, but what you mustn't do is sort the winners array itself; otherwise you'll screw up all that lovely matching between your two arrays.

Question: were you specifically told to use TWO arrays as you've done, or were you simply told that you must use arrays?

Winston

First of all i was given two arrays to use and those are the arrays they provided.
Secondly i want the outcome to be like this

Sujath Mohammed wrote:First of all i was given two arrays to use and those are the arrays they provided.

Fair enough.

Secondly i want the outcome to be like this

OK. Forget about sorting just for the moment. How would you go about that if you were only given one team name (eg, "Balmain")? Write out what you think you'd need to do and show us the code (or simply the steps, written out in English).

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 04:15:13

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:First of all i was given two arrays to use and those are the arrays they provided.

Fair enough.

Secondly i want the outcome to be like this

OK. Forget about sorting just for the moment. How would you go about that if you were only given one team name (eg, "Balmain")? Write out what you think you'd need to do and show us the code (or simply the steps, written out in English).

Winston

As i said the output should display year and team so if i was only given Balmain and the years they won 1908,1909,1910,1945.
Then it should output the team Balmain 4 times for each year so like this
Year Team
1908 Balmain
1909 Balmain
1910 Blamain
1945 Balmain

That's more like it, except you don't really need all those print() statements.

Think about it functionally: given a team name, you need a list of the years it won. You can worry about how you're going to print it out afterwards.

Now, given that you know how to do it for one team, think about how you'd do it for multiple teams

(Hint: you're just repeating the same logic, so I'd strongly suggest putting it in a method).

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 05:01:23

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:Is this what you asking for?

That's more like it, except you don't really need all those print() statements.

Think about it functionally: given a team name, you need a list of the years it won. You can worry about how you're going to print it out afterwards.

Now, given that you know how to do it for one team, think about how you'd do it for multiple teams

(Hint: you're just repeating the same logic, so I'd strongly suggest putting it in a method).

Winston

I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program

Sujath Mohammed wrote:I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program

I'd prefer that you work it out for yourself; you'll learn a lot more, and you'll be much happier when you do so, rather than just being given the answer.

One of the most important aspects of programming is learning how to break problems down into small pieces.
As I understand it, you need to list out teams in alphabetical sequence, along with the years they won the championship; and (and this is MOST important) you need to do it with the arrays you were given.

So: you've now worked out how to list those wins for a specific team.
How do you think you might apply that logic to a list of teams supplied in alphabetical order?

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 06:06:33

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program

I'd prefer that you work it out for yourself; you'll learn a lot more, and you'll be much happier when you do so, rather than just being given the answer.

One of the most important aspects of programming is learning how to break problems down into small pieces.
As I understand it, you need to list out teams in alphabetical sequence, along with the years they won the championship; and (and this is MOST important) you need to do it with the arrays you were given.

So: you've now worked out how to list those wins for a specific team.
How do you think you might apply that logic to a list of teams supplied in alphabetical order?

Winston

I'm not telling you give me the answer i'm just asking if you link me or give a code something similar to what i need

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 06:10:45

0

and one more thing you see i have put this code for my first case but when i enter something thats not in the array it repeats the word "invalid" which i don't want i want it to say valid only once and then ask the user to input a proper team
here's the code:

If you mean a bubble sort: It will work with any array. But the fact is that you DON'T want to sort either of your parallel arrays; otherwise they won't be parallel any more. You need to sort copies of them.

My advice: StopCoding and write down what you think you need to do. You're overthinking this at the moment.

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 08:18:56

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:will bubble work with parallel arrays

If you mean a bubble sort: It will work with any array. But the fact is that you DON'T want to sort either of your parallel arrays; otherwise they won't be parallel any more. You need to sort copies of them.

My advice: StopCoding and write down what you think you need to do. You're overthinking this at the moment.

Winston

You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head
and that the least of my worries if you look at my main menu you can see i also need to sort this into a bar chart as well

Sujath Mohammed wrote:You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head...

Then my advice still applies. You're not going to get it all done, so do what you can; and the most important part of that is making a plan of what you need to do. I can't imagine you're yet at a stage where they're going to fail you for the entire course.

And next time: start earlier.

Winston

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 09:43:23

0

Winston Gutkowski wrote:

Sujath Mohammed wrote:You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head...

Then my advice still applies. You're not going to get it all done, so do what you can; and the most important part of that is making a plan of what you need to do. I can't imagine you're yet at a stage where they're going to fail you for the entire course.

And next time: start earlier.

Winston

Funny that i started working on this assignment a week ago

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31

posted Apr 18, 2013 09:59:01

0

alright so far i came with this code but as it puts into alphabetically it still doesn't update the array
whats is the wrong with the code