0

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[]
labasRyta
  • 13
  • 1
  • 5
  • giving you the complete solution would be wrong. I will suggest that you lookup HashSet and HashMap – Acewin Nov 21 '16 at 18:44
  • @Acewin I'm googling for hours already, when I found something - something else goes wrong... I'm just depressed at the moment, lol... – labasRyta Nov 21 '16 at 18:45
  • Why am I not able to understand what OP is asking? Isn't finding number of words in array just array.length? – user3437460 Nov 21 '16 at 18:45
  • @user3437460 The function is now looking for a max number in the array. I solved it. But when I try to look for word that is entered from console I get an error `cannot convert string to ....` and etc... – labasRyta Nov 21 '16 at 18:49
  • 1
    @labasRyta Then you should also put your error in the question. because it is not completely clear what is the issue you are getting. If you are trying to convert a string to Integer you will need to use method Integer.parseInt – Acewin Nov 21 '16 at 18:51
  • 1
    @labasRyta, provide your tried code with complete error. – Faraz Sultan Nov 21 '16 at 18:51
  • @labasRyta So you have an array of words (in String) and you want to write a function to check whether a specific word exist in that array? – user3437460 Nov 21 '16 at 18:55
  • @Acewin I have updated the post – labasRyta Nov 21 '16 at 19:06
  • @FarazSultan I have updated the post – labasRyta Nov 21 '16 at 19:06
  • 1
    @labasRyta The statement --> String found = in.readLine(); >> needs to be inside a try-catch block. Check this out http://stackoverflow.com/questions/2305966/why-do-i-get-the-unhandled-exception-type-ioexception – Acewin Nov 21 '16 at 19:10
  • @labasRyta basically this error occurs when you not handle the checked Exception. – Faraz Sultan Nov 21 '16 at 19:12
  • 1. It sounds like you're suppose to read the array of words, then call surasti. Not read the word in surasti. 2. What word are you finding as "max"? The biggest words, the last word lexicographically, its not clear to me. – Linus Nov 21 '16 at 19:15
  • @Acewin updated once more – labasRyta Nov 21 '16 at 19:22
  • @Linus yeah that would be better way to do it, but as I said I'm a beginner, don't know how to do It. That's why I'm trying to get some help here. :) – labasRyta Nov 21 '16 at 19:23
  • You need to have a return outside try catch or in both try as well as catch. For example you can return null for failed case from catch block. – Acewin Nov 21 '16 at 19:30
  • @Acewin nah, im giving up... maybe someone will share the code... :/ – labasRyta Nov 21 '16 at 19:42
  • Posted the method in answer – Acewin Nov 21 '16 at 19:45
  • @labasRyta you should not give up like this. You will not learn to code or solve any problem this way. When you start something knew you always have some initial bottlenecks – Acewin Nov 21 '16 at 19:48

2 Answers2

0

Your comparison operator is not correct. Try the following code :

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

You need to pass the array and set the first element of the array as the maximum as by default first. Next iterate through the array and check whether there is a produktas with a higher value of svoris. If so, change the max to point to this new produktas. At the end of the for loop, you will now have the max set to the produktas with the highest value of svoris.

There are few things in the code that you can fix for better quality code.

  1. You don't need to pass the array size to the surasti method. We can just do a G.length (in the code above I have done that).
  2. Best practice is to user Camel case for Java classes. Therefore instead of produktas, use Produktas
  3. Use meaningful names for variables instead of A, G, etc
maheeka
  • 1,983
  • 1
  • 17
  • 25
  • This function is okay, but I want to make It to search for words, that are entered by user from console (there may be more than one word) – labasRyta Nov 21 '16 at 19:27
  • From the code you shared in the question you are comparing a double value. Please elaborate the question as to what you want to do. The last error you see as per the edit, (This method must return a result of type produktas[]) is because you are returning a produktas object instead of an array as defined in the method signature. Notice I have changed it to return a produktas object instead. – maheeka Nov 21 '16 at 19:30
0

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
}
Acewin
  • 1,657
  • 4
  • 17
  • 36
  • I'm using it, but as I'm new it's really hard to understand everything as my main language is not english. Now almost everything works, but ` A[j] = G[i];` I need to insert it as `A[j] = G[i].gautiPav();` but then I get an error `Type mismatch: cannot convert from String to produktas` – labasRyta Nov 21 '16 at 20:07
  • The error is correct (obviously). What is the objective of method surasti. And what are you storing in A – Acewin Nov 21 '16 at 20:18
  • So what should I do? – labasRyta Nov 21 '16 at 20:48
  • @labasRyta check the added code as suitable code. You need to have your code work like that – Acewin Nov 21 '16 at 21:44