0

i have an assignment , i have been working on it all day, i finished it, and it works, but not in the way my teacher wants it, i have been racking my brain for hours without any luck

this is the assignment question

"write a program that inputs five each of which is between 10 and 100 inclusive,as each number i read, display it only if it is not a duplicate number already read, provide for the "worst case," in which all five numbers are different, use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value."

this is the code i wrote

public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
        int sid[] = new int[5];

        int count = 0; 
        int x = 0;
        int num = 0;

        while (x < sid.length) 
            {
            System.out.println("Enter number: ");
            num = input.nextInt();

            if ((num >= 10) && (num <= 100)) {
                boolean digit = false;
                x++;

                for (int i = 0; i < sid.length; i++) 
                 {   if (sid[i] == num)     
                     digit = true;
                 }  

            if (!digit) {

                    sid[count] = num;

                    count++;

            } 

                else

                System.out.printf("the number was entered before \n");

                      }

              else
               System.out.println("number must be between 10 and 100");


            for (int i =0;  i < x; i++) {
                System.out.print(sid[i] +" ");

            }

            System.out.println();  
        } 

        } 
    } 

my problem is in the output the output is like this

    {
        Enter number: 
            11
            11 
            Enter number: 
            21
            11 21 
            Enter number: 
            34
            11 21 34 
            Enter number: 
            11
            the number was entered before
            11 21 34 0 
            Enter number: 
            44
            11 21 34 44 0

}

But my teacher wants the output too look like this, without the 0's at the end, and without changing the value of the array sid from sid[5] to sid[4]

    {
        Enter number: 
            11
            11 
            Enter number: 
            21
            11 21 
            Enter number: 
            34
            11 21 34 
            Enter number: 
            11
            the number was entered before
            11 21 34  
            Enter number: 
            44
            11 21 34 44 

}

i have been trying for hours now without any luck.

ANY help is appreciated.

Thank you.

cheers.

3
  • So.. you should read it... and not display it??? Commented Dec 18, 2013 at 10:56
  • When you print the answer check for '0' and ignore them. Commented Dec 18, 2013 at 10:57
  • You may want to define a constant variable with value 5 for your while loop (or simply replace sid.length with the number 5, since your not using it anywhere else). Not a huge difference, but it is slightly more efficient than having to check for the length of the array each time (since the size of the array remains the same) Commented Dec 18, 2013 at 15:01

4 Answers 4

1

You only have to add an if condition to your output loop.

            for (int i =0;  i < x; i++) {
                if(sid[i]!=0)
                    System.out.print(sid[i] +" ");
            }
Sign up to request clarification or add additional context in comments.

5 Comments

He also want that the size of the array should remain 4 if say one duplicate is there
@Deepak Are you allowed to use an arraylist? Or does it have to be a (standard) array. Arraylists can change size, so they'll only grow if you add something.
@Andreas I dont know exactly if he can use Collection then the Set will be perfect for his scenario
@kai - having understood the question now (thanks to Andreas) .. He wants the smallest array that can be used... Here we still have an array of 5.. we are not printing the value of the duplicate element...
@Deepak Yeah, didn't think about that. Set would be an even better solution
0

When you print the answer check for '0' and ignore them. or else decrements x when you found out the current number is a duplicate.

 for (int i = 0; i < sid.length; i++) 
             {   if (sid[i] == num)     
                 digit = true;
                 x--;
             }  

or

 for (int i =0;  i < x; i++) {
            if(sid[i]!=0)
                System.out.print(sid[i] +" ");
        }

And if it is not necessary to use arrays you can use Sets. Then It would be much easier.

Comments

0

Put the x increment (x++) in the if (!digit)

if (!digit) {

    sid[count] = num;

    count++;

    x++;
}

remove the x-increment (x++) from the if statement below

if ((num >= 10) && (num <= 100)) {
    boolean digit = false;
    x++;
}

that way the zeros are eliminated and the program still runs no matter the number of duplicates tried.

Comments

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

class Example{
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[5];

        int uCount = 0; //number of unique entries
        int entered = 0; //number of entered entries
        int num = 0;

        while(uCount<numbers.length){
            System.out.println("Enter number "+uCount);
            num = scanner.nextInt();
            if (num>=10 | num<=100){
                boolean isRepeat = false;
                entered++;

                for (int i = 0; i < numbers.length; i++) {
                    if(numbers[i]==num) isRepeat = true;
                }
                if(!isRepeat){
                    numbers[uCount] = num;
                    uCount++;
                }
            }
            else {
                System.out.println("Number should be between 10 and 100");
            }
        }
        System.out.println("Array is "+ Arrays.toString(numbers));
    }
}

2 Comments

I assume this code will answer your question
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.