labasRyta labasRyta - 19 days ago 7
Java Question

Change from finding max number to find words in array

I have a function that is searching for max number in the array. I want to make the function to search for more than one word that is entered from console.
As example I enter two words(car,ride) they're added to array and then

"surasti"
function is comparing them if they're in the array.
I have tried to do it on my own, but I'm a started and it seems too hard :(

Function that is searching:

public static produktas[] surasti (produktas G[], int n){
produktas A[] = new produktas[1];
produktas max = G[0];
for (int i=1; i<n; i++)
if (max.gautiSvori()<G[i].gautiSvori()) max = G[i];
A[0]=max;
return A;
}


The code that is calling that function (
A
is the array that you have to search in.):

case 5:
B = surasti(A, n);
System.out.println("Sunkiausias gyvunas yra:");
spausdinti_sar_ekrane(B, B.length);
break;


The produktas class:

class produktas {
private String pavadinimas;
private String salis;
private Double svoris;
private Double kaina;

produktas() {}
produktas(String pav, String salis, double svoris, double kaina){
pavadinimas = pav;
this.salis = salis;
this.svoris = svoris;
this.kaina = kaina;
}



public String gautiPav (){
return pavadinimas;
}

public String gautiSali (){
return salis;
}

public double gautiSvori (){
return svoris;
}

public double gautiKaina (){
return kaina;
}
}


When I try to change the function to this (don't know if its working fine, can't test it):

public static produktas[] surasti (produktas G[], int n){
try{
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
produktas A[] = new produktas[5];
for (int j=0; j<5; j++){
System.out.println("Kokio produkto ieskosime?");
String found = in.readLine();
for (int i=1; i<n; i++){
if (found.equals(G[i].gautiPav())){
A[j] = G[i];
}
}
}
return A;
} catch(IOException ie){
ie.printStackTrace();
}
}


When I try this code I get this error at
public static produktas[] surasti (produktas G[], int n){
line:

This method must return a result of type produktas[]

Answer

For the correctly complied code update your method to have a return in catch block as well.

public static produktas[] surasti(produktas G[], int n) {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
                System.in));
        produktas A[] = new produktas[5];
        for (int j = 0; j < 5; j++) {
            System.out.println("Kokio produkto ieskosime?");
            String found = in.readLine();
            for (int i = 1; i < n; i++) {
                if (found.equals(G[i].gautiPav())) {
                    A[j] = G[i];
                }
            }
        }
        return A;
    } catch (IOException ie) {
        ie.printStackTrace();
        return null; // Expected return from catch block
    }
}

To understand the issue properly start using IDE like eclipse rather than simply using a notepad and compiling code through java/javac

A more suitable code would be like as below. Type q in console when u want to exit from the program.

public static produktas[] surasti(produktas G[], int n) {
    BufferedReader consoleReader = null;
    produktas produktasFound[] = new produktas[5]; // Initalize array to store the produkt found
    try {
        consoleReader = new BufferedReader(new InputStreamReader(System.in));
        boolean exit = false;
        produktas produktasFound[] = new produktas[5];
        int j = 0;//current produktFound index

        while (!exit) {
            System.out.println("Kokio produkto ieskosime?");
            String produktPav = in.readLine();

            if ("q".equals(produktPav)) {
                exit = true;
            } else {
                for (int i=1; i<n; i++){
                    if (found.equals(G[i].gautiPav())){ 
                        A[j] = G[i];
                        j++; 
                    }
                }
            }

            if(j == 5)
                exit = true;
        }
        return produktasFound; // return all the 5 produktas found
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (consoleReader != null) {
            try {
                consoleReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return produktasFound; //If no produkt is found from the array returned is blank
}