java程序使用Scanner读取并打印到文本文件

时间:2014-03-18 23:30:09

标签: java file loops printwriter

我正在尝试读取文本文件,然后在另一个文件中显示输出。 我只能使用Scanner阅读。 input.txt中

3005045 7
3245436 0
7543536 3
8684383 -1

output.txt应该像

ID    Number of Bags   Total Cost
**    **************   **********
如果行李是4或更少,客户每袋支付20.50。 如果行李超过4,则每袋支付15.50。 但如果它是0或负数,则此消息应出现"错误:错误的行李数" 我做了这个程序,但它只工作一次(只读一行)

import java.util.*;
import java.io.*;
import java.io.IOException;

public class Bags {
public static void main(String []args) throws IOException {
    FileInputStream fileinput = new FileInputStream("input.txt");
    FileOutputStream fileoutput = new FileOutputStream("output.txt");
    Scanner infile = new Scanner(fileinput);
    PrintWriter pw = new PrintWriter(fileoutput);
    double total = 0, line = 0;
    int bags = 0, ID = 0, count = 0;
    pw.println("ID\t\tNumber of Bags\t\t\tTotal Cost");
    for(int i = bags; i >= 0; i++, count++){
        ID = infile.nextInt();
        i = infile.nextInt();
        if (i <= 0){
            pw.println(ID + "\tError: Wrong Number of Bags\t\t\t");
            break;
        }
        else if (i <= 4){
            total = (80.50)*i;
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);
            break;
        }
        else {
            total = ((80.50)*4)+((75.50)*(i-4));
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);
            break;
        }
    }
    infile.close();
    pw.close();
}
}

3 个答案:

答案 0 :(得分:0)

毫无疑问,循环只能迭代一次。在每种情况下,您都有break

同样在这种情况下,您不应该使用for循环,尤其不是现在使用它的方式。只要看看它,只有当这个条件i >= 0为假时,你的循环才会结束,这意味着i必须是负数,但即使i成为{{1}像输入中的最后一个数字一样,由于-1,它仍然会在迭代结束时递增,所以你最终会得到i++条件,这是真的,所以循环会尝试再次迭代)< / p>

改为使用

0 >= 0

这样,只有当下一个要阅读时,您才能确保从文件中读取while(scanner.hasNextInt()) 。只需使用预定义的int变量而不是bugs

另一件事是您没有以i格式包含行分隔符。在每个人的末尾添加printf,并且不要使用%n,但要指定希望每个号码都能容纳的空间

\t

答案 1 :(得分:0)

您不应使用i来保存&#34;包的数量&#34;。请参阅第i = infile.nextInt();行。使用另一个变量,那么你应该没问题。此外,你应该继续阅读,直到文件结束,所以你可能无法编写for (int i = 0; i < n; i++)式的循环。

答案 2 :(得分:0)

你不需要循环。此外,您想逐行阅读。以下是您的代码的快速修复:

public class Bags {

public static void main(String[] args) throws IOException {
    FileInputStream fileinput = new FileInputStream("input.txt");
    FileOutputStream fileoutput = new FileOutputStream("output.txt");
    Scanner infile = new Scanner(fileinput);
    PrintWriter pw = new PrintWriter(fileoutput);
    double total = 0, line = 0;
    int bags = 0, ID = 0, count = 0;
    pw.println("ID\t\tNumber of Bags\t\t\tTotal Cost");
    while(infile.hasNext()){

        ID = infile.nextInt();
        int i = infile.nextInt();
        if (i <= 0) {
            pw.println(ID + "\n\t\tError: Wrong Number of Bags\t\t\t");

        } else if (i <= 4) {
            total = (80.50) * i;
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);

        } else {
            total = ((80.50) * 4) + ((75.50) * (i - 4));
            pw.printf("%d\t\t%d\t\t\t\t%.2f", ID, i, total);

        }
    }
    infile.close();
    pw.close();
}
}

Output.txt的

ID      Number of Bags                  Total Cost
3005045 7                               548.503245436
        Error: Wrong Number of Bags         
7543536 3                               241.508684383
        Error: Wrong Number of Bags