-1

So I am extremely new to Java and my assignment has me creating an array 10 indexes in size, copying it, and sorting the copy. These parts I have functioning properly. What they want me to do is prompt the user for a value they wish to search for, and if found, return the index and which array it was found in. This last search part is really messing me up. My code is very sloppy I know, but I am not very good at this yet. The book of course has an example, but it uses a driver class and I'm not entirely sure if that's required in this situation. Thanks for any replies and my apologies if I posted this incorrectly.

http://imgur.com/a/T8t3Q - This is an example of the final output required.

public class MJUnit1Ch9 {

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        double[] numList;                           //list of random numbers
        double[] numListSorted;                     //sorted copy of array
        int numSearch;                              //array search
        int numSearchIndex;                         //position in array
        numList = new double[10];                   //creation of size 10 array

        for (int i = 0; i < 10; i++) {              //create random numbers between 1 and 20
            numList[i] = (int) (Math.random() * 20 + 1);
        }

        numListSorted = Arrays.copyOf(numList, numList.length);  //copy original array
        Arrays.sort(numListSorted);                              //sort copy by API

        System.out.printf("%7s%7s\n", "Unsorted Array", "Sorted Array");
        for (int i = 0; i < numList.length; i++) {
            System.out.printf("%7.2f%7.2f\n", numList[i], numListSorted[i]);
        }
        //*************************************************************************

        //*************************************************************************
        System.out.print("Please enter number to search for:");
    }
}
Juan Carlos Mendoza
  • 5,203
  • 7
  • 21
  • 48
  • Have you tried Binary Search on your array? Check this - It has BS implementation in the first answer: https://stackoverflow.com/questions/19492402/sorting-and-binary-search-using-java – Sergei Sirik Aug 17 '17 at 20:00
  • "Driver" programs are typically just used as a main method that calls code defined in another class. Consider is a demo. You don't need it. You can just write the logic (a simple loop) to search the array in the main method you already have. – Bill the Lizard Aug 17 '17 at 20:04
  • 1
    I have not. From what I could gather the binary search can only be utilized on sorted arrays. Am I incorrect in thinking this? – Jeremy McCaffity Aug 17 '17 at 20:04
  • Maybe this will be helpful: https://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java#2506109 – ElderMael Aug 17 '17 at 20:14

1 Answers1

-1

You can read number using scanner object

stdIn.nextInt()

And for searching you can use Binary search method of Arrays class by passing the number which you read from scanner object

Pavan
  • 167
  • 1
  • 2
  • 12
  • I tried to set numSearch by using stdIn.nextInt() after the string for prompt, but it's the actual searching of two different arrays and the returning of index position for these if they are present. – Jeremy McCaffity Aug 17 '17 at 20:10