
The following code;
import java.io.File; import java.util.*; public class BinarySearch { public static int search(int key, int[] a) { return search(key, a, 0, a.length); } public static int search(int key, int[] a, int lo, int hi) { if (hi <= lo) return -(hi-1); int mid = lo + (hi - lo) / 2; int cmp = a[mid]-key; if (cmp > 0) return search(key, a, lo, mid); else if (cmp < 0) return search(key, a, mid + 1, hi); else{ if (a[mid+1]==key) return search(key,a,mid+1,hi); else{ return mid; } } } public static void main(String[] args) { String name= args[0]; int count=0; try{ Scanner scanner = new Scanner(new File(name)); while(scanner.hasNextInt()) { scanner.nextInt(); count++; } } catch(Exception e){} int a[] = new int[count]; int in=0; try{ Scanner scanner1 = new Scanner(new File(name)); while(scanner1.hasNextInt()) a[in++]=scanner1.nextInt(); } catch(Exception e){} System.out.println(search(Integer.parseInt(args[1]),a)); } }
Do not know how to correct the error; Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at BinarySearch.main(BinarySearch.java:38) Any suggestion would help, Thanks
No hand written and fast answer with explanation

Step by stepSolved in 2 steps

- Explain Every Step of Below Code: public static void main(String args[]) { int count = 0; System.out.println("Enter a sentence :"); Scanner sc = new Scanner(System.in); String sentence = sc.nextLine(); for (int i=0 ; i<sentence.length(); i++) { char ch = sentence.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) { System.out.print(""); } else if(ch != ' ') { count++;} } System.out.println("Number of consonants in the given sentence is "+count); }arrow_forwardCompute the average kids per family. Note that the integers should be type cast to doubles. 427080.2564214.qx3zqy7 1 import java.util.Scanner; N&6 00 2 3 public class TypeCasting { 4 5 7 8 9 10 11 12 13 14 15 16 17 Pun public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int numKidsA; int numKidsB; int numKidsC; int numFamilies; double avgKids; numkidsA scnr.nextInt (); numKidsB = scnr.nextInt (); numkidsC= scnr.nextInt (); numFamilies scnr.nextInt(); * Your solution goes here */ 1 test passed All tests passedarrow_forwardTASK 3 The String class is provided in the Java library. Provide your own Java implementation for the following methods (name the new class MyString): public Mystring(char [] chars); public char charat (int index); int length(); public Mystring substring(int begin, int end); public Mystring toLowercase); public boolean equals (Mystring s); public static Mystring valueof (int i); public int compare(string s); public Mystring substring(int begin); public Mystring toupperCase(); Write a Java test program that tests all methods in the class.arrow_forward
- Assign is Teenager with true if kidAge is 13 to 19 inclusive. Otherwise, assign is Teenager with false. 439894.2564214.qx3zqy7 1 import java.util.Scanner; □NM nor 2 3 public class TeenagerDetector { 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public static void main (String [] args) { Scanner scnr = new Scanner(System.in); boolean isTeenager; int kidAge; kidAge scnr.nextInt(); * Your solution goes here if (isTeenager) { System.out.println("Teen"); } else { System.out.println("Not teen");arrow_forward1) Complete the algorithm below and test it,Give its time complexity as well public class BubbleSort { public static void bubbleSort(int a[], int size) { int outer, inner, temp; for (outer = size - 1; outer > 0; outer--) { // counting down for (inner = 0; inner < outer; inner++) { // bubbling up } } } }arrow_forwardJava - Write a program that removes all non alpha characters from the given input.arrow_forward
- import java.util.Arrays; import java.util.Random; public class Board { /** * Construct a puzzle board by beginning with a solved board and then * making a number of random moves. That some of the possible moves * don't actually change what the board looks like. * * @param moves the number of moves to make when generating the board. */ public Board(int moves) { throw new RuntimeException("Not implemented"); } The board is 5 X 5. You can add classes and imports like rand.arrow_forward*JAVA* complete method Delete the largest valueremoveMax(); Delete the smallest valueremoveMin(); class BinarHeap<T> { int root; static int[] arr; static int size; public BinarHeap() { arr = new int[50]; size = 0; } public void insert(int val) { arr[++size] = val; bubbleUP(size); } public void bubbleUP(int i) { int parent = (i) / 2; while (i > 1 && arr[parent] > arr[i]) { int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; i = parent; } } public int retMin() { return arr[1]; } public void removeMin() { } public void removeMax() { } public void print() { for (int i = 0; i <= size; i++) { System.out.print( arr[i] + " "); } }} public class BinarH { public static void main(String[] args) { BinarHeap Heap1 = new BinarHeap();…arrow_forwardI need help with this code !! import java.util.Arrays;import java.util.Scanner; public class MaxElement {public static void main(String[] args) { //create an object for Scanner class Scanner x = new Scanner (System.in);System.out.print ("Enter 10 integers: ");// create an arrayInteger[] arr = new Integer[10]; // Execute for loopfor (int i = 0; i < arr.length; i++) {//get the 10 integersarr[i] = x.nextInt(); } // Print the maximum numberSystem.out.print("The max number is = " + max(arr));System.out.print("\n"); } //max method public static <E extends Comparable<E>> E max(E[] arr) {E max = arr[0]; // Execute for loop for (int i = 1; i < arr.length; i++) { E element = arr[i];if (element.compareTo(max) > 0) {max = element;}}return max; }}arrow_forward
- Correct my codes in java // Arraysimport java.util.Scanner;public class Assignment1 {public static void main (String args[]){Scanner sc=new Scanner (System.in);System.out.println("Enter mark of student");int n=sc.nextInt();int a[]=new int [n];int i;for(i=0;i<n;i++){System.out.println("Total marks of student in smster");a[i]=sc.nextInt();}int sum=0;for(i=0;i<n;i++){sum=sum+a[i];}System.out.println("Total marks is :");for (i=0;i<n;i++);{System.out.println(a[i]);}System.out.println();}}arrow_forwardimport java.util.Arrays; import dsUtils.WordReader; public class WordFrequencyAnalyzer { /**********************************************************************************/ /* You are not allowed to add any fields to this class beyond the one given below */ /* You may only read in from the file once. This means you may only use a single */ /* word reader object. */ /**********************************************************************************/ // Currently, the field counters is not used. // Your task is to make this class more efficent by storing the word counts // as a symbol table / map / dictionary in the field counters. private SequentialSearchST<String, Integer> counters; private String filename; /** * Stores a count of the number of times any word appears in a file. The file is * read in exactly once at the time time this object is constructed. *…arrow_forwardDefine the method findHighestValue() with a Scanner parameter that reads integers from input until a negative integer is read. The method returns the largest of the integers read. Ex: If the input is 70 65 100 -85 -15 -50, then the output is: 100Note: Negative numbers are less than 0. import java.util.Scanner; public class HighestValueFinder { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int maxVal; maxVal = findHighestValue(scnr); System.out.println(maxVal); }}arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





