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;
   }
   
}

Recommended Answers

All 3 Replies

the error message describes what is to be said:
you're calling the method isMember() without parameters, but this method does not exist.
replace the isMember() in your print by isMember(numbers)

next, I doubt you'll method 'll do what you ultimately want it to do, but I think that'll be quite clear

non-static method isMember(int[]) cannot be referenced from a static context System.out.println(searchValue + ": " + isMember(numbers));

This is the error message I get referencing numbers. It should be clear to me what I am doing wrong, but it is not. Java is unfortunately not one of my strengths. So my isMember method does not make any sense at all?

Thanks for helping!

if does make sense, but it is impossible to call a non-static method from within a static method, unless you create an Object through which you call that method.

so, since you're not using objects, declare that method as:

public static boolean isMember(int[] numbers){
...
}

also, if I'm right about what you're trying to do, you'll need to add the searchValue as a parameter:

if(numbers.length > 0)          {             for(int i = 0; i < numbers.length; i++)                System.out.println(searchValue + ": " + isMember());            System.out.println();         }

you don't need this loop, since the value of searchValue nor the contents of your array change. replace this by:

System.out.println(searchValue + ": " + isMember(numbers, searchValue));

after this, change your method to:

public static boolean isMember(int[] numbers, int searchValue){
//here you can use a loop to check whether or not searchValue
//is a member of numbers, after which you return either true, or false
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.