drjava - Java Type mismatch: cannot convert from java.lang.Object -


i working out of blue pelican java textbook , using drjava. working on lesson 43 big bucks project have use bankaccount class:

public class bankaccount {   public bankaccount(string nm, double amt)   {     name = nm;              balance = amt;           }        public void deposit(double dp)     {         balance = balance + dp;      }           public void withdraw(double wd)     {             balance = balance - wd;      }       public string name;      public double balance; } 

and creat class allow me enter multiple accounts, store them in array list , determine account has largest balance. here code far:

import java.io.*; import java.util.*;  //includes arraylist import java.text.*;  //for numberformat public class bigbucks  {        public static void main(string args[])    {           numberformat formatter = numberformat.getnumberinstance( );     formatter.setminimumfractiondigits(2);        formatter.setmaximumfractiondigits(2);          string name;     list arylst = new arraylist( );                   {                   scanner kbreader = new scanner(system.in);         system.out.print("please enter name whom account belongs. (\"exit\" abort)");        name = kbreader.nextline( );       if( !name.equalsignorecase("exit") )          {         system.out.print("please enter amount of deposit. ");                  double amount = kbreader.nextdouble();             system.out.println(" ");  //gives eye-pleasing blank line          bankaccount myaccount = new bankaccount(name,amount);             arylst.add(myaccount); //add account array list           }             }while(!name.equalsignorecase("exit"));     //search arylist , print out name , amount of largest bank account            bankaccount ba = arylst.get(0);//get first account in list         double maxbalance = ba.balance;         string maxname = ba.name;         for(int j = 1; j < arylst.size( ); j++)       {       //? step through remaining objects , decide 1 has           //largest balance (compare each balance maxbalance)        bankaccount na = arylst.get(j);          double nbalance = na.balance;       string nname = na.name;       if(nbalance > maxbalance)       {         maxbalance = nbalance;         maxname = nname;       }        //? step through remaining objects , decide 1 has    largest balance (compare each balance maxbalance)       //?           }           system.out.println("the accouint largest balance belongs "+ maxname + ".");     system.out.println("the amount $" + maxbalance + ".");   }    } 

however every time compile error

type mismatch: cannot convert java.lang.object bankaccount 

at lines 28 , 35. why error , how can fix it??? appreciated.

you're storing bankaccount objects in plain list.

list arylst = new arraylist( ); 

change specify list of bankaccount objects.

list<bankaccount> arylst = new arraylist<bankaccount>( ); 

when using generics way, compiler error try , add other bankaccount list, won't have cast objects when access list elements.


the alternative (not recommended if you're using java 5 or later) cast objects when access list.

bankaccount ba = (bankaccount)arylst.get(0); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -