I need help fixing my program!! When I run it and search for an a number that is
ID: 3532968 • Letter: I
Question
I need help fixing my program!! When I run it and search for an a number that is actually IN the array, it still says its "NOT FOUND!". Help please!!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class search2 {
public static void main(String[] args) {
int target = 0;
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
int[] data = {5,8,11,6,2,13,3,4,7,10,9,12,1};
//Makes an array of size (20 in this case) of random ints
int size = 20;
data = new int[size];
Random rand = new Random();//Argument is the size limit of the int
int mod = 1000;
for(int x = 0 ; x < size; x++){
data[x]= rand.nextInt()%mod;}
while(target!=-1){
try {System.out.println("Enter the number you want to search or -1 to quit:");
target=new Integer(in.readLine());
if(target!=-1){
if(linearSearch(data, target))
{ System.out.println("FOUND! It is in the array index #"+linearSearchPosition(data, target));}
else {System.out.println("NOT FOUND!");}}}
catch (IOException e) {
e.printStackTrace();
}}
System.out.println("Quit!");}
/** Return true if target appears in the sorted array data. */
public static boolean linearSearch(int[] data, int target) {
for (int i = 0; (i < data.length) && (data[i] <= target); i++) {
if (data[i] == target) {
return true;
}
}
return false;
}
/** Return true if target appears in the sorted array data. */
public static int linearSearchPosition(int[] data, int target) {
for (int i = 0; (i < data.length) && (data[i] <= target); i++) {
if (data[i] == target) {
return i;
}
}
return -1;
}
}
Explanation / Answer
You have initialized your data array 2 times.
Remove the second initialization from your code it works fine!! I have done it for you see below code.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Search2 {
public static void main(String[] args) {
int target = 0;
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
int[] data = { 5, 8, 11, 6, 2, 13, 3, 4, 7, 10, 9, 12, 1 };
while (target != -1) {
try {
System.out
.println("Enter the number you want to search or -1 to quit:");
target = new Integer(in.readLine());
if (target != -1) {
if (linearSearch(data, target)) {
System.out.println("FOUND! It is in the array index #"
+ linearSearchPosition(data, target));
} else {
System.out.println("NOT FOUND!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Quit!");
}
/** Return true if target appears in the sorted array data. */
public static boolean linearSearch(int[] data, int target) {
for (int i = 0; (i < data.length) && (data[i] <= target); i++) {
if (data[i] == target) {
return true;
}
}
return false;
}
/** Return true if target appears in the sorted array data. */
public static int linearSearchPosition(int[] data, int target) {
for (int i = 0; (i < data.length) && (data[i] <= target); i++) {
if (data[i] == target) {
return i;
}
}
return -1;
}
}
output:
Enter the number you want to search or -1 to quit:
11
FOUND! It is in the array index #2
Enter the number you want to search or -1 to quit:-1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.