search method

-1
//crear una estructura de datos que lea los libros de una biblioteca 
//y logremos obtener el nombre del autor del libro y la naturalidad
package libro;

import java.util.Scanner;
public class Libro {


    public static void main(String[] args) {
        Scanner sc= new Scanner (System.in);
        proceso l;
        l = new proceso();

        int i=0,n;
        System.out.println("Cuántos libros desea ingresar:");
        n=sc.nextInt();
        while(i<n){
        l.libros();
        l.nombreA();
        l.naturalidad();
        i=i+1;
        }
    }

}


package libro;

import java.util.Scanner;

public class proceso{

Scanner sc=new Scanner(System.in);
     String libros[],naturalidad[],nombreA[];
     int i=0;
     public void libros()
     {  
        System.out.println("Ingrese el nombre del libro:");
        libros[i]=sc.nextLine();
     }

     public void nombreA()
     {
         System.out.println("Ingrese el nombre del autor:");
         nombreA[i]=sc.nextLine();
     }

     public void naturalidad ()
     {
         System.out.println("Ingrese la naturalidad del libro:");
         naturalidad[i]=sc.nextLine();
     }

     public static String BusquedaSecuencial(){
         String elem;
            for (int i = 0; i < libros.length; ++i)
            {
                if (vector[i] == elem)
                {
                    return i;
                    return -1;
                }
            }
     }

}

This is my exercise but I do not know how to implement the search method. I need to know the author and the naturalness of a book only when the user enters his name.

Thank you very much, if someone can help me, I would appreciate it

    
asked by Emmanuel Arenilla Mendoza 24.08.2018 в 15:28
source

1 answer

0

Hello friend, one way to do your search would be the following:

 public static String BusquedaSecuencial(String nombre){

            for (int i = 0; i < libros.length; ++i)
            {
                if (libros[i] == nombre)
                {
                    return "Autor: " nombreA[i]+" "+"Naturalidad: "+ 
                    naturalidad[i];
                }
            }
     }

This on the assumption that you insert the data in the arrangements in the same position for all.

And in your main you only send the function and show the result.

System.out.println (BusquedaSecuencial("NOMBRE DEL LIBRO"));
    
answered by 24.08.2018 в 16:30