I am having problems getting my code to work properly. I cannot seem to get the results to print and get this error message
"IsMemberMethod.java:59: isMember(int[]) in IsMemberMethod cannot be applied to () System.out.println(searchValue + ": " + isMember());",
but I cannot seem to work around it.
Also, not sure if I am following the logic of the project description in general. I have found that I have issues with boolean expressions for some reason. Any help would be greatly appreciated. Thanks!
import java.util.Scanner;
/**
Project Description:
Write a recursive boolean method named isMember.
The method should search an array for a specified value,
and return true if the value is found, or false if
the value is not found in the array.
Demonstrate the method in a program.
*/
public class IsMemberMethod
{
public static void main(String [] args)
{
int searchValue; // The value to search for
int result; // The search result
String input; // A line of input
char again; // To hold a single character
// The values in the following array are sorted
// in ascending order.
int numbers[] = {7, 14, 15, 17, 20, 21, 32, 52, 55,
67, 72, 85, 88, 89, 90, 99, 100};
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
do
{
// Get a value to search for.
System.out.print("Enter a value to " +
"search for: ");
searchValue = keyboard.nextInt();
// Search for the value
result = binarySearch(numbers, 0,
(numbers.length - 1),
searchValue);
// Display the results.
if(numbers.length > 0)
{
for(int i = 0; i < numbers.length; i++)
System.out.println(searchValue + ": " + isMember());
System.out.println();
}
//System.out.println(searchValue + ": " + isMember());
// Does the user want to search again?
System.out.print("Do you want to search again? " +
"(Y or N): ");
// Consume the remaining newline.
keyboard.nextLine();
// Read a line of input.
input = keyboard.nextLine();
} while (input.charAt(0) == 'y' ||
input.charAt(0) == 'Y');
}
/**
The binarySearch method performs a binary
search on an integer array and returns
true if the value is found..
@param array The array to search.
@param first The first element in the search range.
@param last The last element in the search range.
@param value The value to search for.
@return The subscript of the value if found,
otherwise -1.
*/
public static int binarySearch(int[] array, int first,
int last, int value)
{
int middle; // Mid point of search
// Test for the base case where the
// value is not found.
if (first > last)
return -1;
// Calculate the middle position.
middle = (first + last) / 2;
// Search for the value.
if (array[middle] == value)
return middle;
else if (array[middle] < value)
return binarySearch(array, middle + 1,
last, value);
else
return binarySearch(array, first,
middle - 1, value);
}
/**
The isMember method searchs the array for a specified value,
and returns true if the value is found, or false if
the value is not found in the array.
*/
public boolean isMember (int[]array)
{
int result; // The search result
// Display the results.
if (result == -1)
return false;
else
return true;
}
}