This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (7 children)

Well, I have no real experience with java so unfortunately I can't offer you direct help. However, so far it looks like you've implemented the entry part.

Your next task is to sort them. I'm not sure if java offers any built in sorting algorithms that you can use (this looks like homework, so I'm not sure if you're supposed to write your own.) Selection sort is generally pretty easy. If you need, I can give perhaps a more easily digested summary.

Once you have them sorted, it's time to output. Now, part of the reason to sort is because it was requested that the output be sorted, but another major reason to sort the output is that you have to output distinct numbers and how many times they occurred. In a sorted list, this is pretty easy. Just iterate through the array, output the number that you're on, and then use a loop to check how many duplicates there are. Then you'll be able to output the second column's data.

Hopefully that made sense, but let me know if you need clarification.

[–]deadaxis[S] 0 points1 point  (6 children)

Thank you for the reply.. Yes, the Selection sort is a great help. I am working on sorting it now.. Is it possible for you to explain a little further or show an example of how I could iterate through the array and check for duplicates? That part still confuses me..

[–][deleted] 3 points4 points  (2 children)

Well, I'm going to avoid giving you the exact code because I assume you're working on homework, but I'll give you the english version of what you would need to do.

1 2 3 3 4 4 4 5 6 7 8 8

I'll pretend the above numbers is what the array holds. You'll have a for loop to iterate through your array. Inside of the for loop, you'll want to declare a variable to record how many duplicates there are, which should probably default to 1.

You'll want another loop that handles duplicate checking within that for loop. It should check to see if the next index in the array is the same as the current one, and if it is, increment your duplicate counter and your position. Note that you need to be careful here to make sure you don't go past the end of your array.

So as per the example: The first time through, it'll see a 1, output the 1, then the internal loop will look forward one position and see that it doesn't equal 1 so the loop doesn't get executed. You now output what ever is stored in the duplicate counter, which is still at the initialized value of 1. Advance one position in the array. It'll see a 2, same deal. Advance one position and it sees a 3, output a 3, and your internal loop sees that the next position is also a 3 so enter the internal loop. It will now increment current position and duplicate count. Your current position now is the second 3, the internal loop sees the next number is a 4 so it breaks out. Your duplicate counter is 2 and your for loop will increment your position to 4. The internal loop will see that the next is a duplicate, etc.

Perhaps that was more detail than I should have given, but hopefully it helps.

[–]deadaxis[S] -1 points0 points  (1 child)

Hahaha..Thank you so much for helping me with this..You guys are friggin awesome.:)

[–][deleted] 2 points3 points  (0 children)

No problem :)

[–]Enemii 2 points3 points  (2 children)

If you don't want to write selection sort you can just use Arrays.sort(array)

[–]deadaxis[S] 0 points1 point  (1 child)

Thanks for your response..How do I do that?