我正在尝试将我的文件中的字符串转换为指向所需对象的链接(卡片,对象)作为创建新对象(购买)的参数。
我的卡片存储在一个hashmap中。 我的购买内容存储在散列图中。 我的卡在启动时使用缓冲读卡器加载到程序中。 我的购买将在启动时使用缓冲的阅读器加载到程序中。
错误是
no suitable method found for put(int,cards)
method Map.put(Integer,purchase) is not applicable
(argument mismatch; cards cannot be converted to purchase)
method AbstractMap.put(Integer,purchase) is not applicable
(argument mismatch; cards cannot be converted to purchase)
method HashMap.put(Integer,purchase) is not applicable
(argument mismatch; cards cannot be converted to 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,注册卡扩展卡片)。
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());
}
请帮助,我迷路了:\
答案 0 :(得分:0)
您的代码
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);
}
这里你试图在shoppingMap中放置 cards3对象(卡片类型),其中 purchaseMap 允许购买类型对象。< / p>
如果您的代码类似 purchaseMap.put(receiptId,p2);
,则不会出现错误其中 p2是购买类型对象。
只需用 p2 替换 cards3 。
您的代码应如下所示
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, p2);
}