Community discussion forum

about array..

Tags: java India
  • 8 months ago

    how will i do this one: Create a program that will 1. allow user to input 5 numbers using array. 2. Determine if the list is UNIQUE or NOT UNIQUE NOTE: UNIQUE - if there are no similar elements in the list NOT-UNIQUE - if there are similar elements in the list EXAMPLE: 1 2 3 4 4 It's NOT UNIQUE! please help me..

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 8 months ago

     

    Here's some pseudocode for one method of solving your problem:

    Create the array.

    Get the five inputs and store them in the array.

    Sort the array into ascending order using your favourite sort algorithm.

    Create a Boolean and set it to False.

    Loop through the five variables; if one is the same as the next one, set the Boolean to True.

    If the Boolean is True, the list is Not Unique, otherwise the list is Unique.

  • 8 months ago

     Hi

    Here's a console program that solves the problem.

    Input is evaluated to check if the user has typed in an integers and it it's unique.

    The unique check is done by comparing the new integers with the previous accepted integers.

    /* Input5uniquenumbers.java
     * Created on 22. marts 2008, 23:56
     */
    import java.io.*;
    public class Input5uniquenumbers {  
        public static void main(String[] args) {
           
            int numberOfInputs = 5;
            System.out.println( "\nPlease type in " + numberOfInputs + " integers: " );
           
            InputStreamReader isr = new InputStreamReader( System.in );
            BufferedReader stdin = new BufferedReader( isr );

            String rawInput;
            int inputAsInteger = -1;
            int[] goodInteger = new int[numberOfInputs +1];
            goodInteger[0] = -1;    // index 0 is ignored
           
            boolean inputOk;
            boolean isUnique;
            //
            for ( int intNum = 1; intNum <= numberOfInputs; intNum++) {
            
                inputOk = true;
                rawInput = null;
                System.out.print( "\n" + intNum + ". integer ..");
               
                try {
                     rawInput = stdin.readLine();
                     inputAsInteger = Integer.parseInt( rawInput.trim() );
                } catch (IOException ioError) {
                     inputOk = false;
                     System.err.println( ioError );
                } catch (NumberFormatException ioError ) {
                     inputOk = false;
                     System.err.println( "Not an integer" );
                }
               
                // Evaluation
                if ( inputOk )  {
                    // is the number unique ?
                    isUnique = true;
                    // compares the previous accepted integers with the new one
                    for(int ix = 1; ix < intNum; ix++) {
                        if ( goodInteger[ix] == inputAsInteger ) {
                            isUnique = false;
                            System.err.println( "Not unique" );
                        }
                    }
                    if ( isUnique ) {
                        goodInteger[intNum] = inputAsInteger;
                    } else {
                        intNum--;           // eliminate the for loops intNum++
                    }
                } else {
                    intNum--;
                }
           
            }//ends for loop
           
            System.out.print( "\nYou typed in the following unique integers: " + goodInteger[1]);
            for(int ix = 2; ix <= numberOfInputs; ix++) {
                System.out.print( ", " + goodInteger[ix]  );
            }
        }//ends main()
    }

  • 7 months ago

    thanks for the help 

  • 6 months ago

    Hello everyone 

    Seems you have your problem solved. I came into the thread to see if I could help, but was beaten to the punch.

    Best of luck!! .... Les ..... data recovery software 

Post a reply

Enter your message below

Sign in or Join us (it's free).