I would like to return value 1 if the input matches any of the id elements in the array of objects or else return -1.
For example for
27
vjxiyhc
kyoyfsl
34
oguybhh
cuaxany
10
oxhdjcs
gvhtlzw
19
thamkrf
arijclh
32
cvljyye
heijkiv
34
output should be 1. If the last input is 25, output should be -1.
The code given below is not working properly. It only checks the id value of the last input into the array. If I put 27or 10 or 19 it still returns -1. Only 32 returns 1. Please suggest changes.
public class LibraryDemo
{
public static Library[] searchLibraryById(Library[] objArray, int inputid)
{
int out=0;
for(int i=0;i<objArray.length;i++)
{
if (objArray[i].id==inputid)
{
out=1;
}
else
{
out=-1;
}
}
System.out.print(out);
return objArray;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Library[] objArray=new Library[5];
for(int i=0;i<objArray.length;i++)
{
int id=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
String address=sc.nextLine();
objArray[i]=new Library(id,name,address);
}
int inputid=sc.nextInt();
searchLibraryById(objArray, inputid);
}
}
class Library
{
int id;
String name;
String address;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address=address;
}
public Library(int id,String name, String address)
{
this.id=id;
this.name=name;
this.address=address;
}
}