0

"Write a Java program which asks the user to enter five numbers.

The program should prevent the user from entering the same number more than once. It should also print to the screen the numbers already entered by the user."

The output should become:

Please, enter a number: 3

List: 3

Please enter a number: 16

List: 3 16

Please enter a number: 16

Number is already in the list !!! Try again !!!

List: 3 16

Please enter a number: 23

List: 3 16 23

Of course the numbers don't have to be exactly the ones I said. Please help? What I have so far:

    Scanner in = new Scanner(System.in);

    System.out.print("Please, enter a number: ");
    int num[] = new int[5];

    for (int i = 0; i < num.length; i++)
    {
        num[i] = in.nextInt();
    }

Programming is hard.. I tried to do separate user inputs and listed them that way but I have no idea how to check if a number has already been entered.

Help would be very much appreciated!

mattias
  • 1,991
  • 3
  • 20
  • 27
BbOii
  • 3
  • 4
  • You want a while loop here not a for loop – brso05 Nov 11 '14 at 21:25
  • `int[] num` instead of `int num[]` – Royal Bg Nov 11 '14 at 21:29
  • @Royal Bg; Would you mind telling me the difference? – BbOii Nov 11 '14 at 21:30
  • Nothing wrong with int num[] you are fine... – brso05 Nov 11 '14 at 21:32
  • A discussion over it you can find here http://stackoverflow.com/questions/1200621/declare-array-in-java . In your loop, you need to perform the check before adding to the array. In the answers below it's mentioned how. You either make a List from your array and check with `contains()` and iterate through the array. Or you can start with List, not with array at all. – Royal Bg Nov 11 '14 at 21:34
  • He probably hasn't learned lists so he should iterate over the array... – brso05 Nov 11 '14 at 21:35

3 Answers3

1

I guarantee this isn't how your teacher wants you to do it. BUT

public static boolean arrayContains(String[] arr, String targetValue) {
    return Arrays.asList(arr).contains(targetValue);
}

is the quickest way to do it.

If you cannot do it this way, or don't feel comfortable doing it this way, then a loop through the array would be the other solution. And probably the one that your teacher wants you to use.

public static boolean isInArray(String[] arr, String targetValue) {
    for(String s: arr){
       if(s.equals(targetValue))
          return true;
     }
     return false;
 }
DejaVuSansMono
  • 770
  • 5
  • 14
  • Well he has a lot of problems. But the bottom line of the question says he doesn't know how to check if the number is already entered, which is what I answered. – DejaVuSansMono Nov 11 '14 at 21:27
  • I have never learned anything like that so I'm staying away from it xD – BbOii Nov 11 '14 at 21:27
  • It's a drop in solution. Arrays is here https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html converts arrays to lists and lists has the handy contains method. But I provided a separate solution. Which is probably how the teacher wants you to do it. – DejaVuSansMono Nov 11 '14 at 21:30
1

Here's a basic answer close to your knowledge that I hope you can understand.

for (int i = 0; i < num.length; i++){   
    int current = in.nextInt();         // getting first input for number 'i'

    while( Arrays.asList(num).contains(current)){    // checking if number already exists
       // Number already exist, will loop this untill you give a unique one
       System.out.print("Number already exist. Try again: ");
       current = in.nextInt();
    }

    // Printing the output
    System.out.print("List: ")
    for(var j =0; j < i; j++){
        System.out.print(num[i]+" ");

}

I converted the Array into a List to check if number already exists. If you do not feel confortable using that, you can switch it with a more basic function such as isInArray() that DejaVuSansMono provided.

Alexandru Severin
  • 5,268
  • 9
  • 38
  • 63
0

You want a while loop here not a for loop. A while loop is used when you don't know how many times the loop is going to execute. A for loop is used usually for a fixed number of executions.

Scanner in = new Scanner(System.in);
int num[] = new int[5];
int index = 0;


while(index != 5)
{
    boolean add = true;
    System.out.println("Please, enter a number: ");
    int tempInt = in.nextInt();
    for(int i = 0; i < num.length; i++)
    {
        //check if num[i] == tempInt if it does set add to false because its already in the array
    }
    //if add is true add number to num[index] and increment index
    //if add is false send message to user saying that the number is already in the list
    for(int i = 0; i < index; i++)
    {
        System.out.print(num[i] + " ");
    }
}

This should give you a start. I didn't write everything for you but I gave you a template and some comments. Hopefully this will help you figure out the rest.

brso05
  • 12,634
  • 1
  • 17
  • 37