about array..

java India
  • 13 years 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..

  • 13 years 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.

  • 12 years 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()
    }

  • 12 years ago

    thanks for the help 

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker