user2130057
user2130057

Reputation: 406

Methods aren't returning anything in Java

I wrote a class in Java with 5 methods. Linear search, which returns true if the value is found and false if it's not found. Linear search 2, which returns the location of the value, if found. binary search. which searches for the value within the array as well, print Int array, which prints 10 numbers a time, and selection sort, which sorts the array so I can do the binary search. Everything is compiling fine, but for some reason, none of my methods are returning anything (except the void printIntArray method).

EDIT:


Thanks, guys, I didn't realize I needed that. For some reason I thought it would return the value on its own... Another question, though. The binarySearch method doesn't appear to be doing anything. After the print statement "Searching for 11 in the random array using binary search"...., nothing is printed.


EDIT 2: My binarySearch method wasn't working because I accidentally had mid + 1 for both else statements (else if (key < array[mid]) should have been mid - 1). THANKS so much to everyone! I added the fixes.

public class sortingSearching {

  public static boolean linearSearch (int [] array, int key) {
    for (int i = 0; i < array.length; i++) {
       if (array [i] == key)
           return true;
    }// end for
    return false;
}

public static int linearSearch2 (int [] array, int key) {
   for (int i = 0; i < array.length; i++) {
       if (array [i] == key)
        return i;
    }//end for
    return -1;
}//end linearSearch2

public static boolean binarySearch (int [] array, int key) {
  int left = 0;
    int right = array.length - 1;
    int mid = (left + right) /2;
       while (left <= right) {
           if (array[mid] == key)
               return true;
            else if ( key < array[mid])
               right = mid - 1;
            else 
               left = mid + 1;
            mid = (left + right) /2;
        } //end while
    return false;
}//end binarySearch

public static void printIntArray (int [] array) {
   for (int i = 0; i < array.length; i++) {
       if (i%10 == 0)
           System.out.println();
        System.out.print(array[i] + " ");
    } // end for
}

public static void selectionSort (int [] array) {
   for (int start = 0; start < array.length - 1; start ++) {
       int minI = start;
        for (int i = start + 1; i < array.length; i++)
           if (array[i] < array[start])
               minI = i;
       int temp = array[start];
    array[start] = array[minI];
       array[minI] = temp; 
    }//end for
   } //end selectionSort

public static void main (String args []) {
   int [] array = new int [20];
    for (int i =0; i < array.length; i++)
       array[i] = (int)((Math.random() * 100) + 1);

    //print the array using printArray  
    printIntArray(array);
    System.out.println();
  //use linearSearch to search for 30, 86, and 87
    System.out.println("Searching for 30 in the random array. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
      System.out.println(linearSearch(array, 30));
    System.out.println("Searching for 86 in the random array. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(linearSearch(array, 86));
    System.out.println("Searching for 87 in the random array. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(linearSearch(array, 87));
    //use linearSearch to locate the first occurrences of 25, 80, and 91
    System.out.println("Searching for the location of 25 in the random array. If -1 is " +
     "returned, the number was not found in the array.");
       System.out.println(linearSearch2(array, 25));
    System.out.println("Searching for the location of 80 in the random array. If -1 is " +
     "returned, the number was not found in the array.");
       System.out.println(linearSearch2(array, 80));
    System.out.println("Searching for the location of 91 in the random array. If -1 is " +
     "returned, the number was not found in the array.");
       System.out.println(linearSearch2(array, 91));
    //use selectionSort to sort the array
  selectionSort(array);
    //use binarySearch to search for 11, 28, 74, and 99
  System.out.println("Searching for 11 in the random array using binary search. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(binarySearch (array, 11));
  System.out.println("Searching for 28 in the random array using binary search. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(binarySearch (array, 28));
  System.out.println("Searching for 74 in the random array using binary search. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(binarySearch (array, 74));
  System.out.println("Searching for 99 in the random array using binary search. If true is returned, " +
    "the value was found. If false was returned, the value was not found.");
       System.out.println(binarySearch (array, 99));
} //end main


} //end sortingSearching

Also, I'm sorry all the print statements in the main method are distracting. I thought about taking them out for ease of reading, but I wanted it to be exactly as I've been running it.

Upvotes: 2

Views: 566

Answers (4)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

You must put the sorting/searching method calls inside the println() statements, otherwise the results won't get printed! Like this:

System.out.println(
    "Searching for 30 in the random array. If true is returned, " +
    "the value was found. If false was returned, the value was not found." + 
    linearSearch(array, 30));

Alternatively, store the result in a local variable - but again, you have to pass the variable to println():

boolean result = linearSearch(array, 30);
System.out.println(
    "Searching for 30 in the random array. If true is returned, " +
    "the value was found. If false was returned, the value was not found." + 
    result);

Upvotes: 3

christopher
christopher

Reputation: 27346

linearSearch(array, 30);

They do return something. But DO something with the return value!

boolean value = linearSearch(array, 30);
System.out.println(value);

or even simpler:

System.out.println(linearSearch(array, 30));

In response to your edit

You need to initiate left to 1. You're performing integer division, which can never reach zero. Hence right gets stuck on 1 and left is always less than it.

Upvotes: 4

Aman
Aman

Reputation: 8995

They return what they are supposed to, just that you choose not to do anything with what they return.

You can solve the problem by wrapping the function calls in a System.out.println(), or storing the return values by using ret = yourfunction(params) and displaying ret later.

Upvotes: 2

aran
aran

Reputation: 11850

They don't return anything because you are not assigning the return value to any variable.

Make this:

boolean foo= linearSearch(array, 86);
System.out.println(foo);

or

System.out.println(linearSearch(array, 86));

And so on.

Upvotes: 3

Related Questions