-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.

https://i.sstatic.net/My6BC.jpg - 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:");
    }
}
4
  • Have you tried Binary Search on your array? Check this - It has BS implementation in the first answer: stackoverflow.com/questions/19492402/… Commented Aug 17, 2017 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. Commented Aug 17, 2017 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? Commented Aug 17, 2017 at 20:04
  • Maybe this will be helpful: stackoverflow.com/questions/2506077/… Commented Aug 17, 2017 at 20:14

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.