java中输入类型不匹配解决,Java类型不匹配:无法从java.lang.Object转换

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.

解决方案

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

List aryLst = new ArrayList( );

to

List aryLst = new ArrayList( );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值