I'm working on a problem where I need to use a one-dimensional array to write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, it displays it only if it is not a duplicate of a number already
read. The program should keep asking for input until 5 unique numbers are input. Use the smallest possible array to solve this program. Display the complete set of unique values input after the user enters each new value.

I'm stuck. Everything works correctly except for the nested if loop. It always accepts this for true. Any ideas on how I might rearrange my code to make this work properly? Thanks in advance!
Also, I apologize for the sloppy code. I don't know how to make it translate to look pretty on here.

import java.util.Scanner; 
public class UniqueTest {
	
	public static void main(String[] args) {
		//declare an array with 5 elements
		int num[] = new int[5];
		int index = 0;
		Scanner input = new Scanner(System.in);
		//declare the input object (she didn't do this for us)
		
		
		while(index < num.length){
			System.out.println("Enter an integer between 10 and 100: ");
			//accept a keyboard input and assign it to a variable
			int number = input.nextInt();
						
			if (number>=10&&number<=100)
			{
				++index;
				num[index] += number;
				
				System.out.printf("%d\n", num[index]);
		
				if (number == num[index])
				{
					System.out.println("Value has already been entered.");
				}									
			}		
			 else
			{
				System.out.println("Error: You did not enter a number between 10 and 100.");
			}
		}
	}	
}

you are doing a single check with the if loop

do a for

bool contains = false;
for(int i=0; i<index; i++)
{
  if (number == num[i])
{
System.out.println("Value has already been entered.");
contains = true;
} 
}

if(!contains)
{
num[index] = number;
++index;
}

Thank you very much for your help. That worked out perfectly. Now I just noticed one final problem with my code. Instead of saving each value in the array and displaying it like 88, then 88,53 then 88,53,44 and so on, it just repeats whatever number I have most recently entered. How can I store each number permanently into the array and display it accordingly?

use the same for loop concept, put it at the bottom of the while loop

for(int i=0; i<index; i++)
{
  System.out.println(num[i]);
}

use the same for loop concept, put it at the bottom of the while loop

for(int i=0; i<index; i++)
{
  System.out.println(num[i]);
}

You've been very helpful and I thank you greatly! I apologize for my ignorance, but you should've seen me about 2 months ago! Thanks again.

hey man, it takes time to grasp onto the concept of programming, (althought we all can't admit it :-) ), i commend you on trying and thats what we like to see here, not just give me an answer

keep up the good work and keep showing your effort!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.