Question
Need help to modify my code so that it allows user to keep seaching values in array, then when user enters -1 the program ends this is my code: public class q3a { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random randomGenerator = new Random(); System.out.println(" "+"Enter number of array elements: "); int numTimes = input.nextInt(); int[] array1 = new int[numTimes]; System.out.println("Array Filled: "); for(int i=0; i<16) { System.out.println(array1[i]); } else{System.out.println("Array too big to print. But it's filled. Take my word for it.");} } q3a.bubbleSort(array1); System.out.println(" "+"Enter a number to search for: "); int value = input.nextInt(); if(q3a.binarySearch(array1, value)>0) { System.out.println("Searched "+q3a.binarySearch(array1, value)+" times."); System.out.println("your value was found at position " +q3a.binarySearch(array1, value)); } else{System.out.println("Value not found");} } public static void bubbleSort(int[] array) { Arrays.sort(array); System.out.println(" "+"Array Sorted:"); if(array.length<16) { for( int i=0; i
Explanation / Answer
I am only writing the main method with correction and the remaining code will remain as it is. public static void main(String[] args) { int value; Scanner input = new Scanner(System.in); Random randomGenerator = new Random(); System.out.println(" " + "Enter number of array elements: "); int numTimes = input.nextInt(); int[] array1 = new int[numTimes]; System.out.println("Array Filled: "); for (int i = 0; i 0) { System.out.println("Searched " + q3a.binarySearch(array1, value) + " times."); System.out.println("your value was found at position " + q3a.binarySearch(array1, value)); } else { System.out.println("Value not found"); } } }while(value!=-1); }