BbOii
BbOii

Reputation: 3

Listing out User Inputs

"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!

Upvotes: 0

Views: 46

Answers (3)

Alexandru Severin
Alexandru Severin

Reputation: 6228

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.

Upvotes: 1

brso05
brso05

Reputation: 13232

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.

Upvotes: 0

DejaVuSansMono
DejaVuSansMono

Reputation: 797

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;
 }

Upvotes: 1

Related Questions