Issue
The following error appears in my class Vetor: "Incompatible types: Pessoa cannot be converted to String".
Class Vetor
package Pratique;
public class Vetor {
private Pessoa[ ] A;
private int capacity;
private int size;
public Vetor(int capacity) {
A = new Pessoa[capacity];
this.size = 0;
this.capacity = capacity;
}
public boolean isEmpty() {
if (size==0) {
return true;
} else {
return false;
}
}
public int size() {
return size;
}
public Pessoa get(int i) throws Exception {
if (isEmpty()) throw new Exception ("Lista vazia");
if (i>=size()) throw new Exception ("Posição não existe");
return A[i];
}
public void set(int i, Pessoa n) throws Exception {
if (isEmpty()) throw new Exception ("A lista está vazia!");
if (i>=size()) throw new Exception ("Esta posição não existe!");
A[i]=n;
}
public void locPlace(Pessoa n) throws Exception {
if (size==0)
add(0,n);
else
for (int i=0;i<size;i++)
if(n.getID()>=A[i].getID()){
add(i,n);
break;
}
}
public void add(int i, String n) {
if (size==A.length){
size--;
for (int j=size=i;j>i;j--)
A[j+1]=A[j];
A[i]=n;
size++;
}
}
}
Class Pessoa
package Pratique;
public class Pessoa {
protected String nome;
protected int ID;
public Pessoa (String nome, int ID) {
this.nome = nome;
this.ID = ID;
}
public String getNome() {
return nome;
}
public int getID() {
return ID;
}
}
I trying some solutions, but didn't worked. This error is showing on lines 34 and 38, as you can see in the image. How can I do fix this?
Solution
Look at the parameters of add. N is a String not a Pessoa.
Answered By - Andrew Lazarus
Answer Checked By - Mary Flores (JavaFixing Volunteer)