我真的不知道,如果使用地图将是最好的对于这种情况,但随后又可以给你更容易检查correspoding每个帐户的平衡。
您可以将帐户号码作为密钥的地图和余额作为其相应的值。
Map balancesOfEachAccount = new HashMap<String, Double>()
//with this, you can either treat the account number as double also.
//however, it would be good to treat it as String since you won't be
//doing any special operation for it as a double, you only want the
//account number as something that indicates the balances so string is enough.
Wih关心从文本文件中提取数据,您可以使用FileReader。 link
在朝着分离每一行,为您的地图关键字和值的问题,您可以使用的StringTokenizer的java:link
另外请注意,如果你将要使用的文本文件将只包含2为每一行分隔字符串,否则您需要为其添加额外的处理。
StringTokenizer st = new StringTokenizer("1001 50.67"); //retrieved from text via file reader
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
//implement mapping here,
//1001 as key and 50.67 converted to Double as its value in map
}
//rest of your code
还要注意的是StringTokenizer提供了默认的分隔符,所以当文本文件,改变其格式它是如何分离帐户号码和相应的平衡,你可能需要修改添加到您的解析器为这一个。您也可以指定上面提供的链接上显示的分隔符。
StringTokenizer(String str, String delim)
希望这会有所帮助。
*余额* **不是**整数。并尝试'System.out.println(0.05 + 0.05 + 0.05);'为了理解你为什么要用'BigDecimal'来赚钱。 –