带循环的输入和输出

时间:2013-12-10 02:27:05

标签: java loops input

我需要编写一个程序,从输入文件中获取3个学生信息。在根据扫描的学分数确定他们是全日制学生还是兼职学生之后计算学费,然后将他们所有的信息输出到输出文件。到目前为止,我非常自信大多数代码都可以正常工作,但我遇到了问题。

我们班刚刚介绍了有关循环的材料,我需要确保代码使用这样的。到目前为止,我使用while循环扫描整个文件直到结束。虽然它确实获得了所需的学生信息。它只打印最后一个,所以显然只得到1个,然后用下一个部分覆盖同一个。这是代码。

我或多或少只需要输入和输出的帮助。

输入文件位于

之下
  

Dom Pilorusso
  1037 Waterford Court
  Canonsburg PA 15317
  C937493021
  15   

  Dan Madeupname
  106 Cidar Lane
  McMurray PA 15317
  C927012312
  11   

  史蒂夫阿诺德   281 Maple Drive
  Canonsburg PA 15317
  C482716209
  9个


public class Program4 {

// Sets the prices for full time and part time students.

static final double TUITION_PER_CREDIT = 276.00;
static final double FEE_PER_CREDIT     = 15.00;
static final double SERVICE_PER_CREDIT = 7.09;
static final double FULL_TIME_TUITION  = 3311.00;
static final double FULL_TIME_FEE      = 184.00;
static final double FULL_TIME_SERVICE  = 85.00;


public static void main(String[] args) throws FileNotFoundException
{
    int i                       ;
    String firstName      =null ;
    String lastName       =null ;
    String accountNumber  =null ;
    double creditsTaken = 0     ;
    String address1             ;
    String address, address2, address3, address4, address5, address6;      
    String fileName             ;
    double tuition      = 0     ;
    double fees         = 0     ;
    double total        = 0     ;
    String formatFees           ;
    String formatTotal          ;
    String formatTuition        ;

            //create a scanner object named inFile and assign it the file input.dat
            fileName = JOptionPane.showInputDialog("Please enter the input file name. ");

            Scanner inFile = new Scanner (new FileReader(fileName));

            //create a PrintWriter object named outFile associated with the file output.dat
            PrintWriter outFile = new PrintWriter ("tuitionAndFees.dat");

            //Intended to loop the input until the end of the file.

    while (inFile.hasNext())
    {   firstName     = inFile.next();
        lastName      = inFile.next();
        address       = inFile.next();
        address2      = inFile.next();
        address3      = inFile.next();
        address4      = inFile.next();
        address5      = inFile.next();
        address6      = inFile.next();
        accountNumber = inFile.next();
        creditsTaken  = inFile.nextDouble();
    }  

    //If Else statement to determine if the student is a part time or full time student, and then calculates their bill.
        if(creditsTaken < 12)
        {   tuition = TUITION_PER_CREDIT * creditsTaken;
            fees    =(FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
            total   = tuition + fees;

        }

        else
        {
            tuition = FULL_TIME_TUITION;
            fees    = FULL_TIME_FEE +  FULL_TIME_SERVICE;
            total   = tuition + fees;
        }
       formatTotal   = String.format("%.2f", total);
       formatFees    = String.format("%.2f", fees);
       formatTuition = String.format("%.2f", tuition);

       //Output to file all info, needs fixed.
       outFile.println("Tuition Billing Report ");
       outFile.printf("CWID\t\t"+ "Name\t\t"+ "Credits\t\t"+ "Tuition\t\t"+ "Fees\t\t"+ "Total%n");
       outFile.printf(accountNumber + "\t"+ firstName + " "+ lastName + "\t"+ creditsTaken + "\t\t" + formatTuition + "\t\t" + formatFees + "\t\t" +formatTotal);

       inFile.close();
       outFile.close();

       JOptionPane.showMessageDialog(null, "The program was saved in tuitionAndFees.dat");
    } 
  }

2 个答案:

答案 0 :(得分:1)

您只创建了每个变量中的一个,并且在您正在查看文件的同时,您不断地将值分配给变量并覆盖以前的值。

您有3种选择:

1 - 你为每个变量创建一系列数组并在while内部分配值(这是一个有点糟糕的结构化编程)

2 - 您创建一个代表您的学生实体的类,其中包含所有变量,并在您创建实例的同时创建实例并将每个实例分配到数组位置(Student数组)

3 - 你读了每一行并进行了你想要的处理(有时这会让它变得有点困难,因为你可能需要有很多的os累加器和辅助变量)

我会选择2。

答案 1 :(得分:0)

在现有代码中,只有在完全读取输入文件后才执行写入。这导致了问题。

<强>解决方案:
读取一个学生的数据,计算并将该学生的数据写入输出文件 对所有学生重复同样的事 使用逐行方法处理数据。

不要将所有学生数据存储在数组中,因为它会增加内存使用量,并且在将数据写入输出文件后,数据不会在程序中使用。

下面修改的主要方法:

public static void main(String[] args) throws FileNotFoundException {

    String firstName = null;
String lastName = null;
String accountNumber = null;
double creditsTaken = 0;
String address1;
String address, address2, address3, address4, address5, address6;
String fileName;
double tuition = 0;
double fees = 0;
double total = 0;
String formatFees;
String formatTotal;
String formatTuition;

// create a scanner object named inFile and assign it the file input.dat
fileName = JOptionPane
        .showInputDialog("Please enter the input file name. ");

Scanner inFile = new Scanner(new FileReader(fileName));

// create a PrintWriter object named outFile associated with the file
// output.dat
PrintWriter outFile = new PrintWriter("tuitionAndFees.dat");
// Print headers in output file
outFile.println("Tuition Billing Report ");

outFile.printf("CWID\t\t" + "Name\t\t" + "Credits\t\t" + "Tuition\t\t"
        + "Fees\t\t" + "Total%n");

// Intended to loop the input until the end of the file.

while (inFile.hasNext()) {
    firstName = inFile.next();
    lastName = inFile.next();
    address = inFile.next();
    address2 = inFile.next();
    address3 = inFile.next();
    address4 = inFile.next();
    address5 = inFile.next();
    address6 = inFile.next();
    accountNumber = inFile.next();
    creditsTaken = inFile.nextDouble();

    // If Else statement to determine if the student is a part time or
    // full time student, and then calculates their bill.
    if (creditsTaken < 12) {
        tuition = TUITION_PER_CREDIT * creditsTaken;
        fees = (FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
        total = tuition + fees;

    }

    else {
        tuition = FULL_TIME_TUITION;
        fees = FULL_TIME_FEE + FULL_TIME_SERVICE;
        total = tuition + fees;

    }
    formatTotal = String.format("%.2f", total);
    formatFees = String.format("%.2f", fees);
    formatTuition = String.format("%.2f", tuition);
    outFile.printf(accountNumber + "\t" + firstName + " " + lastName
            + "\t" + creditsTaken + "\t\t" + formatTuition + "\t\t"
            + formatFees + "\t\t" + formatTotal+"\n");

}
inFile.close();
outFile.close();

    JOptionPane.showMessageDialog(null,
            "The program was saved in tuitionAndFees.dat");

}