The program like the title says is supposed to return the largest integer value in an user-inputted integer array, using binary recursion.
And in the case of an empty array, return Integer.MIN_VALUE.
I am kind of stuck on the logic of this binary recursive algorithm.
I know there is errors with the code but I just don't know what errors, and how I am thinking about this wrong.
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int n=0, i; //n will store the user input for the number of integers
//it is set to its default value 0
//i will serve the purpose of a counter
int[] array; //creating a double array
//The line below creates a reference to a new Scanner object
// called "sc". It reads lines from standard input one at a time.
Scanner sc = new Scanner (System.in);
boolean checkException = false;//creates a boolean variable checkException
//and sets it to its default value false
do {
System.out.print ("How many integers would you like to enter? ");
try {
n = sc.nextInt(); // stores the next integer typed by the user in n using Scanner sc
checkException = true;//sets the boolean value to true, will cause
//the do-while loop to exit
}
catch (InputMismatchException e)//catch values that are not integers
{
System.out.println("This is not a correct integer, please try again.");//error message
sc.next(); //clears Scanner sc for next input
checkException = false;//sets boolean value to false, continues the loop
}
catch(ArrayIndexOutOfBoundsException a)//catches an array that has been accessed with an
//illegal index, either negative or greater than or
//equal to the size of the array
{
System.out.println("This is not a correct integer, please try again.");
sc.next();//clears Scanner sc for further input
checkException = false;
}
array = new int[n]; //declares the array variable array, giving it n elements
} while (checkException == false); //remains in loop as long as the boolean variable is false
checkException = false;
for (i=0; i<array.length; i++)
{
do {
System.out.println("Please enter your number:");
try {
array[i]= sc.nextInt();//stores the user inputs into the indexed array elements
checkException = true;
}
catch (InputMismatchException e)//catches non-integer values and display the error message
{
System.out.println("This is not a correct integer, please try again.");
sc.next();
}
} while (checkException == false);
}
findMaxInt(array);
}
public static void findMaxInt (int [] array) {
findMaxInt(array, 0, array.length-1);
}
public static int findMaxInt (int []array, int start, int end){
if (array.length==0)
System.out.println(Integer.MIN_VALUE);
if (start>end)
return -1;
if (array[end]>array[start])
return(array[end]);
if (array [start]>array[end])
return (array[start]);
int mid= (start+end)/2;
return findMaxInt(array, start, mid);
return findMaxInt(array, mid+1, end);
}