Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Searching arrays


Tags:

Hello everyone,
I'm trying to do my final assignment and first time I can't do it! I need some help, please. This is the task: "Write a program that inputs 5 numbers each is between 10 and 100.
As each number is read, store it in an array only if it is not a duplicate of a number already read.
The worst case is where all 5 values are unique. NOTE: 1. You must use at least two methods in this assignment. 2. At least one method should contain an array parameter.".

I know how to search for a certain value in an array. I can find for the first repeated number but 1) how can I do the rest of it and 2) how can I store it in a new array ? 3) how do I transfer my new array from the method to the MAIN body?

Thank you so much.
P.S. See the attachment to my program.

import java.util.Scanner;
 
public class Hwk12

  public static void main(String args[])
  {            
        int numbers[] = new int[5];
        int search;
       
        readNumbers(numbers);
          
        findRepetion(numbers);

//      if (search == -1)
//         System.out.println("There is no repetions.");
//      else
  //       System.out.println("It repeats with number " + search);
//      for (int index = 0; index < search.length; index++)
//              System.out.print(search[index] + "  ");
  
        System.out.println("\nNot-repeated numbers:");
        showArray(numbers);
  }
 
  public static void readNumbers(int numbers[])
  {
        Scanner stdin = new Scanner(System.in);
               
        System.out.println("Enter the numbers between 10 and 100");
               
        for(int i = 0; i < numbers.length; i++)
        {
           System.out.print("Enter " + (i + 1) + " number: ");
           numbers[i] = stdin.nextInt();

//         if (numbers[i] < 10 || numbers[i] >= 100)
//              System.out.println("Enter a different number");
        }
  }
  public static void findRepetion(int array[])
  {    
        int i = 0;
//          element = -1; 
        boolean equal = false;
 
        while(!equal && i < array.length)
        {
           if  (array[i] == array[i+1] ||
                array[i] == array[i+2] ||
                array[i] == array[i+3] ||
                array[i] == array[i+4])
           {
                equal = true;
                for(int j = 0; j < array.length; j++)
                  System.out.print(array[j] + "  ");
           }
           System.out.println("Show element: " + (i + 1));
           i++;
        }
//      return array;
  }
       
  public static void showArray(int numbers[])
  {
        System.out.println("The numbers you entered are:");
          
        for(int j = 0; j < numbers.length; j++)
                System.out.println(numbers[j]);
               
  }
}