i have list of numbers , trying find numbers "58" , "85" in list. should print true if number, , false otherwise. have been trying 58 part far. when run program, prints "true 58" there wrong else statement? can't seem find problem.
import java.util.arrays; public class asgn9 { public static void main(string[] args) { int[] data = { 12, 25, 35, 45, 58, 64, 77, 80, 84, 93 }; int searchedvalue = 58; int searchedvalue2 = 85; keytest(data, searchedvalue, searchedvalue2); } public static void keytest(int[] data, int searchedvalue, int searchedvalue2) { boolean found = false; int low = 0; int high = data.length - 1; int pos = 0; while (low <= high && !found) { pos = (low + high) / 2; // midpoint of subsequence if (data[pos] == searchedvalue) { found = true; } if (data[pos] < searchedvalue) { low = pos + 1; } // in first half else { high = pos - 1; } // in second half } if (found = false) system.out.println("false " + data[pos]); else if (found = true) system.out.println("true " + data[pos]); }//end of keytest }
edit:
i used loop , getting 10 lines should be. each line returning "true 58." tried editing statements in actual search, i'm not sure if necessary.
for (i = 0; < data.length; i++) { while (low <= high && !found) { pos = (low + high) / 2; // midpoint of subsequence if (data[pos] == searchedvalue) { found = true; } else { found = false;} { low = pos + 1; } // in first half if (data[pos] == searchedvalue) {found = true;} else { high = pos - 1; }// in second half if (data[pos] == searchedvalue) {found = true;} else { found = false;} } if (!found) system.out.println("false " + data[pos]); else system.out.println("true " + data[pos]); }//end of
one =
assignment (and test value assigned side-effect). need 2 ==
like
if (found == false) { system.out.println("false " + data[pos]); } else if (found == true) { system.out.println("true " + data[pos]); }
also, i'd prefer shorter boolean not !
like
if (!found) { system.out.println("false " + data[pos]); } else { system.out.println("true " + data[pos]); }