【发布时间】:2017-10-25 03:41:04
【问题描述】:
我正在尝试将通过缓冲读取器输入的文件中的字符串转换为所需对象(卡片、对象)的链接,作为创建新对象(购买)的参数。
- 我的卡片存储在哈希图中。
- 我的购买存储在哈希图中。
- 我的卡在启动时使用缓冲读卡器加载到程序中。
- 我的购买将/试图在启动时使用缓冲阅读器加载到程序中。
错误是
没有找到适合 put(int,cards) 的方法
方法 Map.put(Integer,purchase) 不适用
(参数不匹配;卡不能转换为购买)
方法 AbstractMap.put(Integer,purchase) 不适用
(参数不匹配;卡不能转换为购买)
方法 HashMap.put(Integer,purchase) 不适用
(参数不匹配;卡不能转换为购买)
这是我的采购类:
public class purchase
{
int receiptId;
cards cards1;
int time;
private double men;
private double women;
private double kids;
private double home;
private double sport;
public purchase(int receiptId, cards cards1, double men, double women, double kids, double home, double sport)
{
this.receiptId = receiptId;
this.cards1 = cards1;
this.men = men;
this.women = women;
this.kids = kids;
this.home = home;
this.sport = sport;
}
这里是HashMap的创建:
private static HashMap<Integer, purchase> purchaseMap = new HashMap<Integer, purchase>();
private static HashMap<Integer, cards> map = new HashMap<Integer, cards>();
这里是卡的一种类型代码(basicCard 扩展了 RegisteredCards,registeredcards 扩展了卡)。
public class basicCard extends registeredCards
{
private String name;
private String email;
double balance;
int id;
double points;
public basicCard(String name, String email, double balance, int id, double points)
{
super(name, email, balance, id, points);
this.name = name;
this.email = email;
this.balance = balance;
this.id = id;
this.points = points;
}
这是卡片的 csv .txt 文件
b,basictestnew1,email@email.com,0.0,1,0.0
p,basictestnew1,email@email.com,0.0,2,0.0
a,0.0,3,0.0
这是用于购买的 csv .txt 文件
1,null,10.0,10.0,10.0,10.0,10.0
2,1,10.0,10.0,10.0,10.0,10.0,10.0
3,2,10.0,10.0,10.0,10.0,10.0,10.0
这里是卡片和购买的 BufferedReaders
try
{
BufferedReader in = new BufferedReader(new FileReader("cardData.txt"));
String line;
line = in.readLine ();
while (line != null)
{
String[] field = line.split(",");
if ( field[0] .equals("b") )
{
basicCard b2 = new basicCard(field[1], field[2], Double.parseDouble(field[3]), Integer.parseInt(field[4]), Double.parseDouble(field[5]));
System.out.println("Basic card created after BufferedReader split");
map.put(b2.getid(), b2);
cardsList.add(b2.getid());
line = in.readLine ();
}
else if ( field[0] .equals("p") )
{
premiumCard p2 = new premiumCard(field[1], field[2], Double.parseDouble(field[3]), Integer.parseInt(field[4]), Double.parseDouble(field[5]));
System.out.println("Premium card created after BufferedReader split");
map.put(p2.getid(), p2);
cardsList.add(p2.getid());
line = in.readLine();
}
else if ( field[0] .equals("a") )
{
anonCard a2 = new anonCard(Double.parseDouble(field[1]),Integer.parseInt(field[2]),Double.parseDouble(field[3]));
System.out.println("Anon card created after buffered reader split");
map.put(a2.getid(), a2);
cardsList.add(a2.getid());
line = in.readLine();
}
}
in.close ();
BufferedReader in1 = new BufferedReader(new FileReader("purchaseData.txt"));
String line1;
line1 = in1.readLine();
while (line1 != null )
{
String[] field1 = line.split(",");
int cardInt;
int receiptId;
receiptId = Integer.parseInt(field1[0]);
cards cards3;
cardInt = Integer.parseInt(field1[1]);
cards3 = map.get(cardInt);
purchase p2 = new purchase(Integer.parseInt(field1[0]), map.get(cardInt), Double.parseDouble(field1[2]), Double.parseDouble(field1[3]), Double.parseDouble(field1[4]), Double.parseDouble(field1[5]), Double.parseDouble(field1[6]));
purchaseMap.put(receiptId, cards3);
}
in1.close();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
【问题讨论】:
-
到底出了什么问题?
-
来自异常:方法 Map.put(Integer,purchase) 不适用(参数不匹配;卡片无法转换为购买),您将卡片对象放入购买值,不适用
标签: java object parameters hashmap bufferedreader