# Help with Arrays

• October 9th, 2012, 09:42 PM
xdx
Help with Arrays
please note that I can only use loops
I need to code a program which prompts the user to input integers. When the user enters three unique integers, the program should stop and display the unique integers.
Example

Enter a integer:
1
Enter a integer:
1
Enter a integer:
2
Enter a integer:
1
Enter a integer:
3
First 3 unique numbers entered are: 1 2 3

I need help in figuring out how I can search the recently input value to the previous values and discard them if they are the same (duplicates) using for loops

Code java:

```import java.util.Scanner; public class uniqueinteger {     public static void main(String[] args) {   int userInput, array[]; int i = 0; array = new int[3];   Scanner myScan = new Scanner (System.in);   while (i <= 3){ System.out.println("Enter a non negative integer: "); userInput = myScan.nextInt(); array[i] = userInput; i++;       if (userInput < 0) i--;   }   System.out.println("First 3 unique numbers entered are: " + array[i] + " ");   }   }```
• October 9th, 2012, 10:18 PM
jps
Re: Help with Arrays
Code :

```for each previously saved number... if current number == any previously saved number, reject current number otherwise save current number as unique```
• October 9th, 2012, 11:40 PM
xdx
Re: Help with Arrays
I did figure that out but I have tried many ways to implement it but nothing seems to work so far.
Code java:

``` while (i <= 3){ System.out.println("Enter a non-negative integer:"); userInput = myScan.nextInt(); array[i] = userInput; i++;   for (int j = 1; j<=array.length; j++){ if (array[i] == array[j]) i--; else i++;     } if (userInput < 0) i--;   if (i == 3) break; }```
• October 10th, 2012, 12:39 AM
jps
Re: Help with Arrays
1) I'm not so sure adding a value to the array before it has been validated is a good idea. That would be like having the line to the club,......inside the club... It can get confusing to the reader. Perhaps store the current number in a variable for validating, and if it passes allow it a home in the array, and if not just overwrite it on the next iteration of the loop. What happens when you reach the last number of the array? Wouldn't the test always be true since it is testing the number against itself?

2)if (array[i] == array[j]) Assuming you are testing what you think you are testing.... Does it make sense to i-- or i++ for every element of the array as it is being checked?
The posted algorithm suggests to check the entire array, and then decide to i-- and discard the current number, or i++ and keep the current number as unique.

Code :

```if (userInput < 0) i--;```
Java allows you to leave the curly braces out in this case. Put them in anyway. The program is no different with or without them, the difference is to the reader of the code. Box up these 1-liner if statements just like the rest of the code and keep everything in a neat and orderly style.
Code java:

```if (userInput < 0) i--;   if (i == 3) break; }```
This just looks like crazyness with the indentation so far off and the braces missing.

Start back at the algorithm. Take the sample given in post #2 and break the steps down into smaller steps each. When you can no longer break the steps down it is time to start translating that into code. Make sure you have a full understanding of each step individually from start to finish and don't focus so much on the finished product or the big picture as you write code for each broken down step.