Java类型不匹配:无法从Java .lang. object转换。

时间:2021-05-19 11:56:29

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:

我使用的是blue pelican 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;
}

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.

在第28行和第35行。为什么会出现这个错误,我该如何修复它???任何帮助都是感激。

3 个解决方案

#1


1  

You're storing BankAccount objects in a plain list.

您将BankAccount对象存储在一个简单的列表中。

List aryLst = new ArrayList( );

Change that to specify a list of BankAccount objects.

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

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.

这样使用泛型时,尝试在列表中添加除了BankAccount之外的其他任何东西都是一个编译错误,但是当您访问列表元素时,您不需要转换对象。


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

另一种选择(如果您使用的是Java 5或更高版本)将在访问列表时抛出对象。

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

#2


1  

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

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

List aryLst = new ArrayList( ); 

to

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

#3


0  

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.

我希望这能帮上忙,我在这个项目上遇到了麻烦,至少我能帮你。

#1


1  

You're storing BankAccount objects in a plain list.

您将BankAccount对象存储在一个简单的列表中。

List aryLst = new ArrayList( );

Change that to specify a list of BankAccount objects.

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

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.

这样使用泛型时,尝试在列表中添加除了BankAccount之外的其他任何东西都是一个编译错误,但是当您访问列表元素时,您不需要转换对象。


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

另一种选择(如果您使用的是Java 5或更高版本)将在访问列表时抛出对象。

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

#2


1  

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

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

List aryLst = new ArrayList( ); 

to

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

#3


0  

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.

我希望这能帮上忙,我在这个项目上遇到了麻烦,至少我能帮你。