Arrays Logical error in output...?
import java.util.Scanner;
public class Sal {
public static void main (String args []){
Scanner s=new Scanner(System.in);
String area[]=new String [10];
int pincode[]=new int [10];
int search;
int chk=0;
int p=0;
System.out.println("Enter 10 areas");
for (int i=0;i<=9;i++)
area[i]=s.next();
System.out.println("Enter 10 pincodes");
for (int l=0;l<=9;l++)
pincode[l]=s.nextInt();
System.out.println("Enter Search");
search=s.nextInt();
for (int j=0;j<=9;j++){
if(search == pincode[j] ){
chk=1;
j=p;
break;
}
}
if(chk==1){
System.out.println("Search Found "+"Pincode : "+area[p] );
}else{
System.out.println("Search not Found");
}
}
}
I seem to get the first part of the program right, i.e it tells me if the
search is found or not found. But, after that no matter what I enter as
the pincode I get the very first entered area as the search result.
Suppose I enter 10 areas - pincodes , say
q - 1
w - 2
e - 3
r - 4
t - 5
y - 6
u - 7
i - 8
o - 9
p - 0
If I enter search as "4" I get the output :
Search Found Pincode : q
No comments:
Post a Comment