1

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;
}
}
0

2 Answers 2

2

You need to break out of your loop once the input value is found. out is getting overwritten because you continue to check remaining array indices even after a match has been found

for(int i=0;i<objArray.length;i++)
{
    if (objArray[i].id==inputid)
    {
        out=1;
        break; // found match, break out of loop
    }
    else 
    {
        out=-1;
    }
}
System.out.print(out);
Sign up to request clarification or add additional context in comments.

Comments

1

You are not breaking out of the for Loop after the match. you can do it more efficiently if the array has large no of objects using parallel streams. Refer code below.

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import static java.util.stream.Collectors.toList;

 public static Library[] searchLibraryById(Library[] objArray, int inputid)
    {
        List<Library> lib = Arrays.asList(objArray).parallelStream()
                .filter(obj -> obj.id == inputid).collect(toList());
        int out = lib.size() != 0 ? 1 : -1;
        System.out.println(out);
        return objArray;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.