I've been working long and hard on this and I've come to one error in my binarysearch for a double in an array.
It says on line 109 that the value searchValues might nto have been initialized. Why is that? Also how do I call arrays in INt and Double form through the mthods I've made.
It says on line 109 that the value searchValues might nto have been initialized. Why is that? Also how do I call arrays in INt and Double form through the mthods I've made.
import java.util.Scanner; public class ArrayDemo { public static void main(String[] arg) { int[] value = {5,2,4,3,1}; int result; int searchValue; String again; System.out.println("Array Integer"); for (int index = 0; index < value.length; index++) System.out.println(value[index] + " "); System.out.println(); ArrayOperations.selectionSort(value); System.out.println("Sorted Integer"); for (int index = 0; index < value.length; index++) System.out.println(value[index] + " "); System.out.println(); result = ArrayOperations.sequentialSearch(value, 5); if(result == -1) { System.out.println("5 was not found."); } else{ System.out.println("5 was found in " + result); } System.out.println(); result = ArrayOperations.sequentialSearch(value, 6); if(result == -1) { System.out.println("6 was not found."); } else{ System.out.println("6 was found in " + result); } System.out.println(); Scanner binary = new Scanner(System.in); do{ System.out.println("Enter a whole number between 1 and 5."); searchValue = binary.nextInt(); result = ArrayOperations.binarySearch(value, searchValue); if (result == -1) { System.out.println(searchValue + " was not found"); } else { System.out.println(searchValue + " was found in " + result); } binary.nextLine(); System.out.print("Would you like to search for another number? Y or N "); again = binary.nextLine(); } while (again.charAt(0) == 'y' || again.charAt(0) == 'Y'); double[] values = {8.0,1.0,7.0,2.0,6.0,3.0,5.0,4.0}; int results; double searchValues; String search; System.out.println("Array Double"); for (int index = 0; index < values.length; index++) System.out.println(values[index] + " "); System.out.println(); ArrayOperations.selectionSort(values); System.out.println("Sorted Double"); for (int index = 0; index < values.length; index++) System.out.println(values[index] + " "); results = ArrayOperations.sequentialSearch(values, 8.0); System.out.println(); if(results == -1) { System.out.println("8.0 was not found."); } else{ System.out.println("8.0 was found in " +results); } System.out.println(); results = ArrayOperations.sequentialSearch(values, 9.0); if(results == -1) { System.out.println("9.0 was not found."); } else{ System.out.println("9.0 was found in " +results); } System.out.println(); Scanner binary1 = new Scanner(System.in); do{ System.out.println("Enter a whole number between 1.0 and 8.0."); searchValue = binary1.nextInt(); result = ArrayOperations.binarySearch(values, searchValues); if (results == -1) { System.out.println(searchValues + " was not found"); } else { System.out.println(searchValues + " was found in " + results); } binary1.nextLine(); System.out.print("Would you like to search for another number? Y or N "); search = binary1.nextLine(); } while (search.charAt(0) == 'y' || search.charAt(0) == 'Y'); } } ______________________________________________________________________________________________________ public class ArrayOperations { //Sequential Search public static int sequentialSearch(int [] array, int value) { int index, start; boolean found; index = 0; start = -1; found = false; while (!found && index < array.length) { if (array[index] == value) { found = true; start = index; } index++; } return start; } //Sequential Double public static int sequentialSearch(double [] array, double value) { int index, start; boolean found; index = 0; start = -1; found = false; while (!found && index < array.length) { if (array[index] == value) { found = true; start = index; } index++; } return start; } //Sort Selection public static void selectionSort(int[] value) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (value.length-1); startScan++) { minIndex = startScan; minValue = value[startScan]; for(index = startScan + 1; index < value.length; index++) { if(value[index] < minValue) { minValue = value[index]; minIndex = index; } } value[minIndex] = value[startScan]; value[startScan] = minValue; } } //Sort Double public static void selectionSort(double[] array) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (array.length-1); startScan++) { minIndex = startScan; minValue = (int) array[startScan]; for(index = startScan + 1; index < array.length; index++) { if(array[index] <minValue) { minValue = (int) array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } //Binary Search public static int binarySearch(int[] array, int value) { int first, last, middle, position; boolean found; first = 0; last = array.length -1; position = -1; found = false; while(!found && first <= last) { middle = (first + last) / 2; if(array[middle] == value) { found = true; position = middle; } else if(array[middle] > value) last = middle - 1; else{ first = middle + 1; } } return position; } //Binary Double public static int binarySearch(double[] array, double value) { int first, last, middle, position; boolean found; first = 0; last = array.length -1; position = -1; found = false; while(!found && first <= last) { middle = (first + last) / 2; if(array[middle] == value) { found = true; position = middle; } else if(array[middle] > value) last = middle - 1; else{ first = middle + 1; } } return position; } public static int getTotal(int[] units) { int total = 0; for(int value : units) total += value; return total; } public static double getTotal(double[] units) { double total = 0.0; for(double value : units) total += value; return total; } public static int getAverage(int[] scores) { int total = 0; int average; for(int index = 0; index < scores.length; index++) total += scores[index]; average = total / scores.length; return average; } public static double getAverage(double[] scores) { double total = 0.0; double average; for(int index = 0; index < scores.length; index++) total += scores[index]; average = total / scores.length; return average; } public static int getHighest(int[] numbers) { int highest = numbers[0]; for(int index = 1; index < numbers.length; index++){ if(numbers[index] > highest) highest = numbers[index]; } return highest; } public static double getHighest(double[] numbers) { double highest = numbers[0]; for(int index = 1; index < numbers.length; index++){ if(numbers[index] > highest) highest = numbers[index]; } return highest; } public static int getLowest(int[] numbers) { int lowest = numbers[0]; for(int index = 1; index < numbers.length; index++){ if(numbers[index] < lowest) lowest = numbers[index]; } return lowest; } public static double getLowest(double[] numbers) { double lowest = numbers[0]; for(int index = 1; index < numbers.length; index++){ if(numbers[index] < lowest) lowest = numbers[index]; } return lowest; } }