Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Campbell Ritchie wrote:Did you look at the links about how bad parallel arrys are, which I posted yesterday?
Why are you using shorts? The default type for integers is the int.
Sujath Mohammed wrote:I am using arrays because the assignment says i have to...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Sujath Mohammed wrote: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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Sujath Mohammed wrote: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...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Sujath Mohammed wrote: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...
I understand what you want; I'm saying: given your two arrays, how would you do it for ONE team?
You need to ShowSomeEffort (←click). Nobody here is simply going to hand you the answer.
Winston
Sujath Mohammed wrote:So i had one team i would write a code like this:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Sujath Mohammed wrote:So i had one team i would write a code like this:...
That's not what I asked. Obviously, you can simply write a custom program for an individual team.
One more time: given your existing arrays, how would you extract the winning years for ONE team?
Winston
Sujath Mohammed wrote:Is this what you asking for?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Sujath Mohammed wrote:here's the code:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Sujath Mohammed wrote:will bubble work with parallel arrays
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
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...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Sujath Mohammed wrote:alright so far i came with this code but as it puts into alphabetically it still doesn't update the array
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I still think your principal problem is using parallel arrays rather than creating a Winner class.Sujath Mohammed wrote: . . . whats is the wrong with the code . . .
Consider Paul's rocket mass heater. |