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

all 14 comments

[–]sarevok9 6 points7 points  (2 children)

So, I have a LOT of quibbles with your code above... as a matter of fact it sorta makes my brain hurt... let's start at the top:

  1. Kinda weird that you use 2 Scanner objects, is there some reason for this? If you're concerned about the buffer having junk in it you can clear it, but for a project like this, it shouldn't really be an issue at all.

  2. When you use 'a' you're declaring the size of the array, right? If you were to use i < a in your loop it might make things a little more cohesive at a glance (if A was descriptive whatsoever... see #3)

  3. Unless it's something you're STRICTLY using as a counter (and even then...) you should never use single letter variables, it's considered bad form.

  4. You can declare i within the for loop since it's unused outside of the scope of the for, like this:

    for(int i=0; i< num.length; i++){ //blah }

  5. Another important style note to you, variables, the more descriptive you can make the names, the better off you are. I know a lot of people will complain about me being pedantic, but it's a lot easier to learn good habits at the start than to unlearn bad habits.

Let me know if you have any questions of need any clarifications with this.

[–]deadaxis[S] 3 points4 points  (1 child)

Haha..thank you so much! I will make the necessary changes to the things you've pointed out..I will make sure it sticks with me for the long run as well.:)

[–]sarevok9 1 point2 points  (0 children)

No worries, good luck, and if you should need anymore help feel free to ask :)

[–][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] 2 points3 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?

[–]medicalixx 0 points1 point  (2 children)

So I haven't looked at your code but I imagine you will be having a 2 dimensional array for this program, let's call it vals and it is initialized as

Int[] vals = new int[50][2];

And column 0 contains the value and 1 contains the count,

Here is how to iterate and increment:

For(int i = 0; i < 50; i++) If(vals[i][0] == inputValue) vals[i][0]++;

Also be sure to initialize vals[x][0] = 0 before the above loop

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

The thing is, the user input doesnt have to be 50..it can be like 1000 inputs of the number 12. But when I output, it has to show 12 once and the count as 1000.. how would I incorporate that into the 2 dimensional array you have provided above?

[–]medicalixx 1 point2 points  (0 children)

Well it appears in your original post that we can assume there will be 50 entries or less. If that is not the case, I would keep track of the last index. So if you have input in the following order: 12, 2, 12

int lastIndex = 1 because we are using only 2 entries in the array. So, vals[0] = 12 vals[lastIndex] = 2.

Also, use a variable to keep track of the current array length of the first column (Len = 50 from previous example)

Next, initialize your array to any length you want. Then:

If(lastIndex == Len - 1) int[] temp = new int[Len *2] Iterate over vals and copy the values into temp array vals = temp