Subject - Data Structure Please help me to debug my error. Thank you in advance! import java.util.Scanner; public class LinearSearchDemo { private static int linearSearch(int[] numbers, int key) { for (int i = 0; i < numbers.length; i++) { if (numbers[i] == key) { return i; } } return -1; // not found } // Main program to test the linearSearch() method public static void main(String[] args) { int[] numbers = { 2, 4, 7, 10, 11, 32, 45, 87 }; System.out.print("NUMBERS: "); for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } System.out.println(); Scanner scnr = new Scanner(System.in); System.out.print("Enter an integer value: "); int key = scnr.nextInt(); int keyIndex = linearSearch(numbers, key); if (keyIndex == -1) { System.out.println(key + " was not found."); } else { System.out.printf("Found %d at index %d.\n", key, keyIndex); } }
Subject - Data Structure
Please help me to debug my error. Thank you in advance!
import java.util.Scanner;
public class LinearSearchDemo {
private static int linearSearch(int[] numbers, int key) {
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == key) {
return i;
}
}
return -1; // not found
}
// Main program to test the linearSearch() method
public static void main(String[] args) {
int[] numbers = { 2, 4, 7, 10, 11, 32, 45, 87 };
System.out.print("NUMBERS: ");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println();
Scanner scnr = new Scanner(System.in);
System.out.print("Enter an integer value: ");
int key = scnr.nextInt();
int keyIndex = linearSearch(numbers, key);
if (keyIndex == -1) {
System.out.println(key + " was not found.");
}
else {
System.out.printf("Found %d at index %d.\n", key, keyIndex);
}
}
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 6 steps with 5 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)