java help

Apr 11, 2005
2,826
0
0
New Jersey
#1
yes i'm back to leech off someone else for help with my class. got this one problem where you enter 10 numbers into an array and it's supposed to do a println of the array showing no duplicates. any help is greatly appreciated seeing this is due by midnight tonight :( here's what i've got...

package Assignment3;

import javax.swing.JOptionPane;

public class P6_5 {

public static void main(String[] args){
//initialize array
int[] Number = new int [10];
int a=0;
int x=0;

//fill array
for (int i=0; i<Number.length; i++){
String numString = JOptionPane.showInputDialog (null, "Enter a number");
Number = Integer.parseInt(numString);
a = Number;

//no dupes
boolean foundDupe=false;
for (int j=0; j<Number.length; j++){
if (Number[j] == a)
foundDupe=true;
}

if (!foundDupe){
Number[x] = a;
x++;
}

//display result
System.out.print(Number + " ");
}
}
}


i can get the array to print but the dupe checker is not working.
 

poopiebottoms

Sparkling Wiggles Lover
Aug 23, 2002
793
1
356
1989 - Baba Booey!
#2
Try starting your inner for loop (j) on index 1.

Also it seems every time you go through the loop you are re-filling the array.
So, you fill it. compare the first item to itself. It will always be a duplicate of itself. therefore nothing gets put into the "cleaned" array.

Offsetting the loop indices, you assume the first item is not a dupe, since one thing can't be a dupe of nothing else. Therefore you check the first item against the second, and so on.

try writing out amemory map of the array on a whiteboard or piece of paper.

by the way, what is your course of study? comp sci?
 
Apr 11, 2005
2,826
0
0
New Jersey
#3
Try starting your inner for loop (j) on index 1.

Also it seems every time you go through the loop you are re-filling the array.
So, you fill it. compare the first item to itself. It will always be a duplicate of itself. therefore nothing gets put into the "cleaned" array.

Offsetting the loop indices, you assume the first item is not a dupe, since one thing can't be a dupe of nothing else. Therefore you check the first item against the second, and so on.

try writing out amemory map of the array on a whiteboard or piece of paper.

by the way, what is your course of study? comp sci?
thanks man. it's computer information systems. part of the business school my "concentration" is web design so i don't have much trouble with xhtml/html but i don't enjoy doing database stuff with sql and my java class.

edit: on a side note i didn't have any trouble doing the other 7 problems he had assigned i just wished he wouldn't lecture on the material thursday for a assignment he assigns monday due friday.
 

poopiebottoms

Sparkling Wiggles Lover
Aug 23, 2002
793
1
356
1989 - Baba Booey!
#4
Cool. did you get it to work? If you did, I'd be interested in what your solution looked like.

I know how those midnight deadlines get. Sometimes the answer takes until 12:30 to work itself out.
I'm in CS, and we use java in at least three of our main classes. The last of which we build a MMORPG like WoW in java.

Try using eclipse IDE, if you're not already for java. It makes it a lot easier to work with.
 
Apr 11, 2005
2,826
0
0
New Jersey
#5
Cool. did you get it to work? If you did, I'd be interested in what your solution looked like.

I know how those midnight deadlines get. Sometimes the answer takes until 12:30 to work itself out.
I'm in CS, and we use java in at least three of our main classes. The last of which we build a MMORPG like WoW in java.

Try using eclipse IDE, if you're not already for java. It makes it a lot easier to work with.
nah it had to be in by 12 but it's all good. there were 7 other programs we had to hand in so a small part of one won't be a big deal. and we're using netbeans IDE.
 

thrawn42

Hackmaster Jones Sr.
Jul 26, 2005
1,443
0
281
Seattle
#6
Sorry to be the ass that's late to the party, but if you still care (you know for that learning stuff):

1) Your second loop looking for duplicates' condition should be j<i. That way while you're inserting into the array you're only looking up to just before the ith array member (which you just inserted) and also because the latter loop indices will be 0 (or at least last I checked that's what uninitialized primary types get in Java).

2) As long as you don't care about having duplicates in the array then you can forego that second insert that starts with the condition if ( !foundDupe ). That's actually what is messing you up as well. You see x does not get incremented when you have a duplicate so that the next time you do have one, the xth indice in the array can get overwritten. In fact I'd say just do something like if ( !foundDupe ) System.out.print(Number + " "); and be done with it.

There are a multiple ways you could have handled the solution. For instance you could have let the user enter a number and then instead of automatically inserting it do that duplicate check right there to allow for insertion or not (keeping a counter of how many you inserted). Then when the 10 numbers are finished being entered you could just print out the array up to the number of unique numbers you'd inserted.
 

p1nk0

Registered User
Jul 27, 2005
28
0
0
#7
If you've already covered the java.util collections in class the easiest method would be to use a Set to eliminate dupes.

HashSet printSet = new HashSet();
for(int i = 0; i < 10; i++){
if(!printSet.contains(inputArray){
System.out.println(inputArray);
}
printSet.add(inputArray);
}
 

poopiebottoms

Sparkling Wiggles Lover
Aug 23, 2002
793
1
356
1989 - Baba Booey!
#8
If you've already covered the java.util collections in class the easiest method would be to use a Set to eliminate dupes.

HashSet printSet = new HashSet();
for(int i = 0; i < 10; i++){
if(!printSet.contains(inputArray){
System.out.println(inputArray);
}
printSet.add(inputArray);
}


Because the Set type automatically eliminates dupe, by definition? Cool.

thrawn: I agree with your index correction. That's a better algorithm. It was late, and I was rushing through to find his bug.
 

p1nk0

Registered User
Jul 27, 2005
28
0
0
#9
Because the Set type automatically eliminates dupe, by definition? Cool.
Yep. In fact if you aren't required to store the data in an array, just add to the set as you are reading input and iterate over the set to print.

In the example case, you aren't really using the uniqueness property of Set but the fact that you can perform a lookup in a HashSet quickly ( O(log n) )
 

poopiebottoms

Sparkling Wiggles Lover
Aug 23, 2002
793
1
356
1989 - Baba Booey!
#10
Yep. In fact if you aren't required to store the data in an array, just add to the set as you are reading input and iterate over the set to print.

In the example case, you aren't really using the uniqueness property of Set but the fact that you can perform a lookup in a HashSet quickly ( O(log n) )
I'll have to read that API.

Now I know who to PM with homework questions. :D

Do you work in the field?
 

p1nk0

Registered User
Jul 27, 2005
28
0
0
#12
In the example case, you aren't really using the uniqueness property of Set but the fact that you can perform a lookup in a HashSet quickly ( O(log n) )

Brain Fart. Hash lookups are actually O(1), even better.
 

thrawn42

Hackmaster Jones Sr.
Jul 26, 2005
1,443
0
281
Seattle
#13
Haha. Lovely utility libs. They take away all the "fun."
 
Apr 11, 2005
2,826
0
0
New Jersey
#14
thanks for all the help guys. haven't learned hash yet unfortunately but p1nk0 i may be pm'ing you in the future if i have any issues. thanks.