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]
# 1
What do you mean by "so that i don't have to enter the same number again"?
floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 2
like if i enter 5 and then for the next line i enter 5 again.............it will prevent it
qwert1a at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 3
I still don't understand your problem. Here is the only place you enter a number:c.print ("Enter the number of songs: ");int max = c.readInt ();All other input is the title of the songs.
floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 4

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

qwert1a at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 5

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

floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 6
where would i put this
qwert1a at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 7
Instead of your current for loop.
floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 8
i can't seem to get it...........can you put it in teh code and show accordingly..........sorry thanks
qwert1a at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 9
Sorry but I don't provide coded solutions. It is your homework/assignment not mine.What can't you get? Post updated code, any error messages and a clear explanation of what you don't understand.
floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 10
like i dont know where to put it and what to put accordingly
qwert1a at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...
# 11

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.

floundera at 2007-7-12 19:08:55 > top of Java-index,Java Essentials,Java Programming...