How to prevent number duplication in Java.
How do i prevent number duplication in this program. Basically so that i don't have to enter the same number again
here's the code:
import java.awt.*;
import hsa.Console;
publicclass BubbleSort_Strings
{
static Console c;// The output console
publicstaticvoid main (String[] args)
{
c =new Console ();
c.println ("\t\t\tIPOD Downloads\n\n");
c.print ("Enter the number of songs: ");
int max = c.readInt ();
c.println ();
String song[] =new String [max];
String tempSong;
for (int i = 0 ; i <= max - 1 ; i = i + 1)
{
c.print ("Enter the name of song " + (i + 1) +": ");
song [i] = c.readLine ();
c.println ();
}//end for i
//sort the song names in the array alphabetically
for (int x = 0 ; x < max - 1 ; x++)
{
for (int y = x + 1 ; y < max ; y++)
{
if (song [x].compareTo (song [y]) > 0)
{
tempSong = song [x];
song [x] = song [y];
song [y] = tempSong;
}//end if
}//end for y
}//end for x
c.println ("Your songs have now been sorted:");
c.println ();
for (int z = 0 ; z <= max - 1 ; z = z + 1)
{
c.println (song [z]);
}//end for z
}// main method
}// BubbleSort_Strings class
[2808 byte] By [
qwert1a] at [2007-11-27 7:28:47]

here how about this:
// The "BubbleSort_Numbers" class.
import java.awt.*;
import hsa.Console;
public class BubbleSort_Numbers
{
static Console c;// The output console
public static void main (String[] args)
{
c = new Console ();
c.print ("Enter the number of items: ");
int max = c.readInt();
int item[] = new int [max];
int temp;
for (int i = 0 ; i <= max - 1 ; i = i + 1)
{
c.print ("Enter a number for the array: ");
item [i] = c.readInt ();
} //end for i
for (int x = 0 ; x < max - 1 ; x++)
{
for (int y = x + 1 ; y < max ; y++)
{
if (item [x] < item [y])
{
temp = item [x];
item [x] = item [y];
item [y] = temp;
} //end if
} //end for y
} //end for x
c.println ("The numbers have been sorted.");
for (int z = 0 ; z <= max - 1 ; z = z + 1)
{
c.println (item [z]);
} //end for z
} // main method
} // BubbleSort_Numbers class
OK, having the correct code helps.
You could use a temp Set. They only allow unique numbers, duplicates are ignored. Or you could write another method to check if the entered number is present or not. BTW you will need to use a while loop.
while(array is not full) {
get number
if(number is unique) { // method call
add number to array
increment a counter
}
}
P.S. i < max is simpler
for (int i = 0 ; i <= max - 1 ; i = i + 1)
{
c.print ("Enter a number for the array: ");
item [i] = c.readInt ();
} //end for i
This is what you currently have. So you need to replace that with the while loop that I gave you the pesudecode for. All you need to do is take the mostly English description of what I said and turn it into Java syntax. It isn't that difficult. I'll give you a start.
while(....) {
// read user input
if(isNumberUnique(....) {
// add to array
// increment counter
}
}
private boolean isNumberUnique(....) {
for(....) {
if(....) {
return false;
}
}
return true;
}
Now all you have to do is fill in the blanks.