【发布时间】:2016-03-18 08:36:11
【问题描述】:
我正在尝试编写一个程序来接收输入和输出文件,其中包含硬币列表和这些硬币的数量。
import java.io.*;
import java.util.*;
public class CookieJar {
public static void cashingIn(File input, File output){
try{
Scanner in = new Scanner(input);
PrintWriter writ = new PrintWriter(output);
double sum = 0;
if(in.hasNext()){
String next = in.nextLine();
Scanner help = new Scanner(next);
while(in.hasNextLine()){
int y = Integer.parseInt(next.substring(0, 1));
if(next.contains("pennies")){
sum += y*0.01;
}
if(next.contains("dimes")){
sum += y*.1;
}
if(next.contains("quarters")){
sum += y*.25;
}
if(next.contains("nickles")){
sum += y*.05;
}
if(next.contains("penny")){
sum += .01;
}
if(next.contains("dime")){
sum += .1;
}
if(next.contains("nickle")){
sum += .05;
}
if(next.contains("quarter")){
sum += .25;
}
if(sum == 0){
String find = String.format("%s", "You have no money in the jar");
writ.println(find);
}
String fixer = String.format("$%sf", sum);
writ.println("You have " + fixer + " in the jar");
}
help.close();
}
else{
String find = String.format("%s" , "You have no money in the jar");
writ.println(find);
}
in.close();
writ.close();
}catch(IOException e){
}
}
}
我在使用此代码时遇到的问题是,我无法从文件中获取硬币数量,并无法确定每种硬币的类型。这是我正在使用的测试方法的示例。
@Test
public void test3() {
try {
// create file
File input = folder.newFile( "input.txt" );
File output = folder.newFile( "output.txt" );
PrintWriter write = new PrintWriter( input );
write.println( "32 nickels" );
write.println( " 1" );
write.println( " nickel 42" );
write.println( "quarters 1 penny" );
write.println( "1 quarter 23 pennies 16" );
write.println( "" );
write.println( "dimes 1 dime 1 dime 1 dime 1 dime" );
write.close();
// invoke program
CookieJar.cashingIn( input, output );
// verify file results
assertTrue ( "Output file does not exist", output.exists() );
Scanner scan = new Scanner( output );
String expected = "You have $14.64 in the jar";
assertTrue ( "Unexpected end of file: expected \"%s\"" + expected, scan.hasNext() );
String actual = scan.nextLine();
assertEquals( "Incorrect result", expected, actual );
assertFalse ( "File contains more data than expected", scan.hasNext() );
scan.close();
}
catch (IOException e) {
fail( "No exception should be thrown" );
}
}
如果我进入千位,我还必须使数字带有逗号。我可能对帮助我所需的内容不够具体,所以如果您需要更多说明,请询问。感谢您的任何帮助。我对格式化很困惑。
【问题讨论】:
-
不要使用
double来表示钱,它不能准确地表示所有值(例如,0.1不能表示)。请改用BigDecimal。 -
包含“季度”的行也包含“季度”,因此您将重复计算所有金额(除了便士)
-
你只检查下一个字符串一次,所以当你有行 write.println("dimes 1 dime 1 dime 1 dime 1 dime" );在您的测试代码中出现,它将运行以下 if 块一次: if(next.contains("dimes")){ sum += y*.1; } if(next.contains("dime")){ sum += .1;但我猜你需要在每次出现字符串时计算它。您还假设该数字仅在字符串的第一个字符中找到: int y = Integer.parseInt(next.substring(0, 1));当情况显然不是这样时
-
正如@AndyTurner 建议的那样,您将重复计算大多数复数数量,因为 quarter 在 quarters 中,dime 在 dimes 等。更重要的是使用
int y = Integer.parseInt(next.substring(0, 1));,您只允许在每行的开头使用 1 位数字。您的测试数据似乎与该假设相矛盾。 -
@Draken 但OP也没有考虑数量何时出现在单位之前的行上,例如示例中为“1”和“镍”。
标签: java file split string-formatting stringtokenizer