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

為{幸葍}努か 提交于 2020-01-03 03:01:43

问题


I am working out of the blue pelican java textbook and using drjava. I am currently working on the Lesson 43 Big Bucks project basically I have to use this 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 also creat another class that will allow me to enter multiple accounts, store them in an array list and then determine which account has the largest balance. Here is my code so 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( ); 
    do         
    {            
      Scanner kbReader = new Scanner(System.in);  
      System.out.print("Please enter the name to whom the account belongs. (\"Exit\" to abort)"); 
      name = kbReader.nextLine( );
      if( !name.equalsIgnoreCase("EXIT") )   
      {
        System.out.print("Please enter the amount of the deposit. ");         
        double amount = kbReader.nextDouble();    
        System.out.println(" ");  //gives an eye-pleasing blank line 
        BankAccount myAccount = new BankAccount(name,amount);    
        aryLst.add(myAccount); //add account to array list    
      }        
    }while(!name.equalsIgnoreCase("EXIT"));
    //Search aryList and print out the name and amount of the largest bank account       
    BankAccount ba = aryLst.get(0);//get first account in the list    
    double maxBalance = ba.balance;    
    String maxName = ba.name;    
    for(int j = 1; j < aryLst.size( ); j++)  
    {
      //? Step through the remaining objects and decide which one has    
      //largest balance (compare each balance to maxBalance) 
      BankAccount na = aryLst.get(j);   
      double nBalance = na.balance;
      String nName = na.name;
      if(nBalance > maxBalance)
      {
        maxBalance = nBalance;
        maxName = nName;
      }

      //? Step through the remaining objects and decide which one has    largest balance (compare each balance to maxBalance)
      //?      
    }      
    System.out.println("The accouint with the largest balance belongs to "+ maxName + ".");
    System.out.println("The amount is $" + maxBalance + ".");
  }   
}

However every time i compile it i get this error

Type mismatch: cannot convert from java.lang.Object to BankAccount

at lines 28 and 35. Why do i get this error and how can I fix it??? Any help is appreciated.


回答1:


You're storing BankAccount objects in a plain list.

List aryLst = new ArrayList( );

Change that to specify a list of BankAccount objects.

List<BankAccount> aryLst = new ArrayList<BankAccount>( );

When using generics this way, it will be a compiler error to try and add anything other than a BankAccount to your list, but you won't have to cast objects when you access list elements.


The alternative (not recommended if you're using Java 5 or later) would be to cast the objects when you access the list.

BankAccount ba = (BankAccount)aryLst.get(0);



回答2:


You are creating an arraylist of raw objects. Rather you should create arraylist of BankAccount objects. Change this

List aryLst = new ArrayList( ); 

to

List <BankAccount>aryLst = new ArrayList<BankAccount>( ); 



回答3:


Heres what I typed to get the correct printout as stated in the project itself.

    public static void main (String[] args)
   {
      NumberFormat formatter = NumberFormat.getNumberInstance();
      formatter.setMinimumFractionDigits(2);
      formatter.setMaximumFractionDigits(2);
      String name;
      ArrayList aryLst = new ArrayList();
      do 
      {
        Scanner in = new Scanner(System.in);
        out.print("Please enter the name to whom the account belongs. (\"Exit\" to abort)");
        name = in.nextLine();

        if(!name.equalsIgnoreCase("EXIT"))
        {
          out.print("Please enter the amount of the deposit. " );
          double amount = in.nextDouble();
          out.println(" ");  // gives an eye-pleasing bank line
          BankAccount acct = new BankAccount(name, amount);
          aryLst.add(acct);
        }

      }while(!name.equalsIgnoreCase("EXIT"));

      //Search aryList and print out the name and amount of the largest bank account
      BankAccount ba = (BankAccount) aryLst.get(0);
      double maxBalance = ba.balance;
      String maxName = ba.name;
      for(int j = 1; j < aryLst.size( ); j++)  
      {
      //? Step through the remaining objects and decide which one has    
      //largest balance (compare each balance to maxBalance) 
      BankAccount na = (BankAccount) aryLst.get(j);   
      double nBalance = na.balance;
      String nName = na.name;

      if(nBalance > maxBalance)
      {
        maxBalance = nBalance;
        maxName = nName;
      }

      //? Step through the remaining objects and decide which one has    largest balance (compare each balance to maxBalance)
      //?      
    }      
    System.out.println("The account with the largest balance belongs to "+ maxName                + ".");
    System.out.println("The amount is $" + maxBalance + ".");

    }

I hope this helps, I had trouble with this same project and the least I could do is help you out.



来源:https://stackoverflow.com/questions/22901508/java-type-mismatch-cannot-convert-from-java-lang-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!