Java类型不匹配:无法从java.lang.Object转换

时间:2014-04-06 23:57:41

标签: java drjava

我正在使用蓝鹈鹕java教科书并使用drjava。我目前正在研究第43课Big Bucks项目,基本上我必须使用这个BankAccount类:

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

并创建另一个类,允许我输入多个帐户,将它们存储在数组列表中,然后确定哪个帐户的余额最大。到目前为止,这是我的代码:

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 + ".");
  }   
}

但是每次编译它都会出现此错误

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

在第28和35行。为什么我会收到此错误,我该如何解决?任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

您将BankAccount个对象存储在普通列表中。

List aryLst = new ArrayList( );

更改它以指定BankAccount个对象的列表。

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

以这种方式使用generics时,尝试将除BankAccount之外的任何内容添加到列表中将是一个编译器错误,但是当您访问时,您不必投射对象列表元素。


替代方案(如果您使用的是Java 5或更高版本,则不推荐)是在您访问列表时强制转换对象。

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

答案 1 :(得分:1)

您正在创建原始对象的arraylist。相反,你应该创建BankAccount个对象的arraylist。改变这个

List aryLst = new ArrayList( ); 

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

答案 2 :(得分:0)

下面是我输入的内容,以获得项目本身所述的正确打印输出。

    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 + ".");

    }

我希望这会有所帮助,我在同一个项目中遇到了麻烦,而我能做的最少就是帮助你。