Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Scanner;
- public class BinarySearch {
- public static void main(String[] args) {
- Scanner menuScan = new Scanner(System.in);
- int index;
- int[] intArray = new int[12];
- intArray[0] = 12;
- intArray[1] = 13;
- intArray[2] = 32;
- intArray[3] = 35;
- intArray[4] = 46;
- intArray[5] = 68;
- intArray[6] = 71;
- intArray[7] = 73;
- intArray[8] = 88;
- intArray[9] = 94;
- intArray[10] = 99;
- intArray[11] = 100;
- String[] critterArray = new String[10];
- critterArray[0] = "Chimaera";
- critterArray[1] = "Clownfish";
- critterArray[2] = "Dugong";
- critterArray[3] = "Eel";
- critterArray[4] = "Fangtooth";
- critterArray[5] = "Grenadier"; //not the soldier, the fish!
- critterArray[6] = "Isopod";
- critterArray[7] = "Lobster";
- critterArray[8] = "Viperfish";
- critterArray[9] = "Whale";
- String[] critterTypeArray = new String[10];
- critterTypeArray[0] = "Chimaera, fish";
- critterTypeArray[1] = "Clownfish, fish";
- critterTypeArray[2] = "Dugong, Pokemon";
- critterTypeArray[3] = "Eel, fish";
- critterTypeArray[4] = "Fangtooth, fish";
- critterTypeArray[5] = "Grenadier, fish";
- critterTypeArray[6] = "Isopod, crustacean";
- critterTypeArray[7] = "Lobster, crustacean";
- critterTypeArray[8] = "Viperfish, fish";
- critterTypeArray[9] = "Whale, mammal";
- System.out.println("Please enter the designated number of the method you want to access:"
- + "/n Enter 1 for a boolean integer check" //foundInt method
- + "/n Enter 2 to search the index number of an integer" //binarySearch method
- + "/n Enter 3 for a boolean String check on a list of sea creatures" //foundString method
- + "/n Enter 4 to search the index number of a sea creature");//binarySearch method, vString
- int choice = menuScan.nextInt();
- if (choice == 1) {
- System.out.println("Please enter the number you wish to search:");
- int searchTerm = menuScan.nextInt();
- if(foundInt(intArray, searchTerm)){
- }
- }
- if (choice == 2) {
- System.out.println("Please enter the number you wish to search:");
- int searchTerm = menuScan.nextInt();
- index = binarySearch(intArray, searchTerm);
- if (index != -1) {
- System.out.println("I found it at index number: " + index);
- } else {
- System.out.println("I am sorry, this number is not in our list. Please try again later");
- }
- }
- if (choice == 3) {
- System.out.println("Please enter the name of the sea creature you wish to search:");
- String searchTerm = menuScan.nextLine();
- // }
- // if (choice == 4) {
- //
- // System.out.println("Please enter the name of the sea creature you wish to search:");
- // String searchTerm = menuScan.nextLine();
- //
- // index = binarySearch(critterTypeArray, searchTerm);
- // if (index != -1) {
- // System.out.println("I found it at index number: " + index);
- // } else {
- // System.out.println("I am sorry, this critter is not in our list. Please try again later");
- // }
- } else {
- System.out.println("Invalid entry; Please try again later.");
- }
- }//end main
- public static boolean foundInt(int intArray[], int searchTerm) {
- int start, end, midPt;
- start = 0;
- end = intArray.length - 1;
- while (start <= end) {
- midPt = (start + end) / 2;
- if (intArray[midPt] == searchTerm) {
- return true;
- } else if (intArray[midPt] < searchTerm) {
- start = midPt + 1;
- } else {
- end = midPt - 1;
- }
- }
- return false;
- }//end foundInt
- static int binarySearch(int intArray[], int searchTerm) {
- int start, end, midPt;
- start = 0;
- end = intArray.length - 1;
- while (start <= end) {
- midPt = (start + end) / 2;
- if (intArray[midPt] == searchTerm) {
- return midPt;
- } else if (intArray[midPt] < searchTerm) {
- start = midPt + 1;
- } else {
- end = midPt - 1;
- }
- }
- return -1;
- }//end binarySearch
- public boolean foundString(String critterArray[], String searchTerm) {
- int start, end, midPt;
- start = 0;
- end = critterArray.length - 1;
- while (start <= end) {
- midPt = (start + end) / 2;
- System.out.println("Testing the results of compareTo: "+critterArray[midPt].compareTo(searchTerm));
- if (critterArray[midPt] == searchTerm) {
- return true;
- } else if (critterArray[midPt].compareTo(searchTerm) < 0) {
- start = midPt + 1;
- } else {
- end = midPt - 1;
- }
- }
- return false;
- }//end foundString
- //
- // public int binarySearch (String critterTypeArray[], int searchTerm){
- //
- // **Due to numerous and bizarre errors this method has been scrapped.**
- //
- // }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment