labasRyta
labasRyta

Reputation: 13

Type mismatch: cannot convert from String to

I'm getting an error like this: Type mismatch: cannot convert from String to produktas ... I'm looking for the solution everywhere, but It seems too difficult for me. Would appriciate any help

My function is:

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].gautiPav(); // error line
                }
            }
        } 
        return A; 
    } catch(IOException ie) {
        ie.printStackTrace();
    }
    return null;
}

And my array class looks like:

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

Upvotes: 0

Views: 3470

Answers (2)

Ironcache
Ironcache

Reputation: 1759

Following discussion in the chat, it seems like you want to return A as produktas, but write/view the guatiPav() method where you reference A. You either need to override toString() if you want A to be represented differently than a series of "random" output:

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

    @Override
    public String toString() {
        return guatiPav(); // or "return pavadinimas;"
    }
}

Or you want to call gautiPav() directly wherever you're referencing the elements of A. I highly recommend the latter approach, as an Object's toString() should be descriptive of the Object, not a single parameter it is comprised of.

Upvotes: 0

Adam
Adam

Reputation: 36703

A is an array of "produktas". You are trying to assign a string into it, that is the String that is returned by your gautiPav() method.

Are you sure you didn't mean to write this instead?

A[j] = G[i]; // error line

If you're seeing strings like this: name.produktas@60e53b93 then you should override the Object.toString() method to return a more human readable string, a typical example might look like this. If you're using any modern IDE such as Eclipse there is a helper for this, for Eclipse: Source, Generate toString()...

@Override
public String toString() {
    return String.format("[produktas: %s]", pavadinimas);
}

Upvotes: 2

Related Questions