java - ClassCastException casting error -


i'm getting error on code , dont understand why it's happening:

for(pacote<par<string, double>> pacote : pacotes) /snippet of code error occurs/ 

trying cast to

 public class pacote <e> {      private int capacidade;     private arraylist <e> pacote;      public pacote (int capacidade){          pacote= new arraylist <> (capacidade);         this.capacidade=capacidade;     }     public par (a itema, b itemb){       this.tipo=itema;       this.peso=itemb;   }    public primeiro(){        return tipo;   }    public b segundo () {        return peso;   }  } 

public class pacote {

private int capacidade; private arraylist <e> pacote;  public pacote (int capacidade){      pacote= new arraylist <> (capacidade);     this.capacidade=capacidade; }  public int getcapacidade (){      return capacidade; }  public int getnumitems (){      return pacote.size(); }  public boolean estacheio () {      return getnumitems()<capacidade-1; }  public boolean empacota (e item){     if (estacheio())         return false;     else {          pacote.add(item);         return true;     }        }  public list<e> items(){     list <e> list = pacote;     return list; } 

code around line happening:

public static void main(string[] args) throws ioexception {         // recebe os dados de input         scanner sc = new scanner(system.in);         system.out.println("introduza o ficheiro com lista de items empacotar:");         string fich = sc.nextline();         bufferedreader reader = new bufferedreader(new filereader(fich));         system.out.println("introduza capacidade dos pacotes:");         int capacidade = integer.parseint(sc.nextline());          // cria os pacotes         list<pacote<par<string, double>>> pacotes =                  gestorpacotes.criapacotes(reader, capacidade);         reader.close();          // mostra informacao dos pacotes         system.out.println("pacotes formados:");         int index = 0;         for(pacote<par<string, double>> pacote : pacotes) {  /error here/             system.out.println("pacote " + index++ + ":"); 

and i'm getting "exception in thread "main" java.lang.classcastexception: pack.par cannot cast pack.pacote", not sure why. can help?

 public static list<pacote<par<string, double>>> criapacotes( bufferedreader filereader, int capacidadepacotes) throws ioexception {         list retorno = new arraylist <> (6);         string s;         while ((s=filereader.readline())!=null){             retorno.add(parseitem(s));         }         return retorno;      } 

your declaration of par incorrect. suppose want inner class, haven't declared class.

public par (a itema, b itemb){   this.tipo=itema;   this.peso=itemb; } 

this constructor, treated constructor surrounding class, pacote, not par, not have class par...


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -