Java类型不匹配:无法从java.lang.Object转换 [英] Java Type mismatch: cannot convert from java.lang.Object
问题描述
我正在使用蓝鹈鹕java教科书并使用drjava。我目前正在开展第43课Big Bucks项目,基本上我必须使用这个BankAccount课程:
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
第28和35行。为什么我会收到此错误,如何解决?任何帮助都表示赞赏。
at lines 28 and 35. Why do i get this error and how can I fix it??? Any help is appreciated.
推荐答案
您正在创建原始对象的arraylist。相反,你应该创建 BankAccount
对象的arraylist。更改此
You are creating an arraylist of raw objects. Rather you should create arraylist of BankAccount
objects. Change this
List aryLst = new ArrayList( );
到
List <BankAccount>aryLst = new ArrayList<BankAccount>( );
这篇关于Java类型不匹配:无法从java.lang.Object转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!