【问题标题】:Java Type mismatch: cannot convert from java.lang.ObjectJava 类型不匹配:无法从 java.lang.Object 转换
【发布时间】:2014-05-19 01:14:59
【问题描述】:

我正在研究 blue pelican java 教科书并使用 drjava。我目前正在从事第 43 课大钱项目,基本上我必须使用这个 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 行。为什么会出现此错误,我该如何解决?任何帮助表示赞赏。

【问题讨论】:

  • 第 28 行和第 35 行是什么?这些行可能都缺少(BankAccount) 演员表。

标签: java drjava


【解决方案1】:

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

List aryLst = new ArrayList( );

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

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

以这种方式使用generics 时,尝试将BankAccount 以外的任何内容添加到您的列表中会出现编译器错误,但您在访问列表元素时不必强制转换对象。


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

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

【讨论】:

    【解决方案2】:

    您正在创建一个原始对象的数组列表。相反,您应该创建 BankAccount 对象的数组列表。改变这个

    List aryLst = new ArrayList( ); 
    

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

    【讨论】:

      【解决方案3】:

      这是我为获得项目本身所述的正确打印输出而键入的内容。

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

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

      【讨论】:

        猜你喜欢
        • 2018-08-04
        • 2017-07-15
        • 2020-04-23
        • 2020-03-29
        • 2013-11-05
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多