Need help to modify my code so that it allows user to keep seaching values in ar
ID: 3658490 • Letter: N
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<array1.length; i++)
{ array1[i] = randomGenerator.nextInt(1000);
if(array1.length<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<array.length; i++)
{ System.out.println(array[i]);
}
}
else{System.out.println("Array too big to print. But it's sorted. Take my word for it.");} }
public static int binarySearch(int[] array, int value)
{//Go through all of the array!
int count = 0;
for(int i = 0; i<array.length; i++)
{ count++;
if(array[i] == value)
{ return count;
}
}
return -1;}}
Proeper outputs