Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:34:22+00:00 2026-06-01T14:34:22+00:00

in my java class we were learning about arrays and this question came up.

  • 0

in my java class we were learning about arrays and this question came up. I have tried to solve it and can’t seem to fulfill the requirements. I can read in the user inputs and have it limited to only 5 elements (one of the other requirements), also the values have to be between 10 and 100 I have also done that. But I cannot seem to “not print” the duplicate values. The array accepts the duplicate values. They don’t have to be taken out, just not printed. Here is my code so far:

import java.util.Arrays;
import java.util.Scanner;

public class ArrayTest {

static Scanner in = new Scanner(System.in);

public static void main(String[] args) {

    int size = 5;
    int InpNum[] = new int[size];

    for (int i = 0; i < InpNum.length; i++){

    while (InpNum[i] <= i){

        System.out.println("Please type a number between 10 and 100: ");
        InpNum[i] = in.nextInt();

        while (InpNum[i] < 10 || InpNum[i] > 100){
            System.out.println("Error: Please type an integer between 10 and 100: ");
            InpNum[i] = in.nextInt();
        }

        Arrays.sort(InpNum);

        System.out.println(Arrays.toString(InpNum));

        }

        while (Search(InpNum, i) == true){
            System.out.println("ERROR: Please enter a number that is not a duplicate of the other numbers you have entered");
            InpNum[i] = in.nextInt();
        }

    }

}

// I can't seem to implement the method below in a useful manner.

    public static boolean Search(int InpNum[], int searchedNum) {

    for(int i : InpNum) {

        if (i == searchedNum) {

            return true;
        }
    }

    return false;
}
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:34:23+00:00Added an answer on June 1, 2026 at 2:34 pm

    I would consider restructuring your application.

    Instead of placing the number the user inputs into the array immediately, store it in a local variable. Then run all the checks you need to run, and add it to the array only if it passes all of them.

    You should only have one while loop in the whole program (the outer one). All those others are greatly confusing the issue and making the problem much harder than it has to be.

    So, in psudo-code:

    int index = 0;
    while (true)
    {
      int num = in.nextInt();
    
      // if not between 10 and 100, continue
      // if it would make the array larger than 5, continue
      //    (or perhaps break out of the loop, since we've filled the array)
      // if it is already in the array, continue
    
      // all the checks passed, so add it to the array!
      InpNum[index++] = num;
    }
    

    As a side note, what you really need is a Set. This is a collection which is guaranteed to have no duplicates and allows you to answer the question “do I contain this value?” in an efficient manner using the method Set.contains( Object ).

    So I would create a TreeSet or HashSet and put every number the user types into it. Your Search function would then simply be a one liner calling contains( searchedNum ) on your set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently started learning Java. I have a question which is more about
I'm learning about text processing in Java for a class and the example in
I'm 14 and have been learning java for about 4/5 months. I'm coding a
I'm starting with Java and I'm learning about setters, getters and encapsulation. I have
I'm learning about deadlocks in Java, and there's this sample code from Sun's official
I am learning Java for a test (tomorrow) and am wondering about a question
I’m just learning Java and trying to have an array of a class. When
I have just started learning basics about Graphics2D class, So far I am able
I'm currently learning about class inheritance in my Java course and I don't understand
Learning Javascript and have a question about global variables. From my reading, most recommend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.