将.txt文件读入并行数组

时间:2015-09-02 18:07:43

标签: java arrays

我的程序需要一些帮助。使用类似的示例程序,我能够获得我需要做的所有事情的基本布局。现在我的主要问题是我的代码是否正确第一部分。我希望它将我的数据文件中的数据读入并行数组。我包含了我的整个程序以供参考布局,但是现在我只是想将数据文件读入一个数组。任何帮助是极大的赞赏。如果需要更多信息或我的格式错误,请告诉我。

数据文件(patient.txt):

11111,史密斯,诺里斯,甲状腺,1000.00
11112,奥巴马,诺里斯,LASEK,500.00
11113,胡佛,诺里斯,牙科,100.00
11114,以上的价格,诺里斯,LASEK,500.00
11115,勒托,诺里斯,甲状腺,1000.00
22221,克拉克,债券,甲状腺,1000.00
22222,长安,债券,LASEK,500.00
22223,登特,债券,牙科,100.00
22224,尼克松,债券,LASEK,500.00
22225,华盛顿,债券,牙科,100.00
33331,琼斯,李,牙科,100.00
33332,罗斯,李,LASEK,500.00
33333,盖茨,李,甲状腺,1000.00
33334,约翰逊,李,甲状腺,1000.00
33335,卡特,李,牙科,100.00
程序(类是Patient_Reports) `

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
public class Patient_Reports {
        public static void main(String[] args)
        {
            int selection;
            String pnumber;
            Patient_Reports patient = new Patient_Reports();
            patient.start_system();
            pnumber = patient.menu();
            selection = Integer.parseInt(pnumber);
            while(selection !=3)
            {
                if(selection==1)
                    patient.All_Information_Report();
                else
                if(selection==2)
                    patient.Surgeries_Doctor_Report();
                else
                if(selection==3)
                    patient.Surgeries_Type_Report();
                else
                if(selection==4)
                    patient.Doctor_Fees_Report();
                else
                if(selection==5)
                    patient.Average_Fees_Report();
                pnumber = patient.menu();
                selection = Integer.parseInt(pnumber);
            }//while loop
            patient.Exit();
            System.exit(0);
        }//main method
    //Read Data File
    int count=-1,i;
    int    [] id = new int [10];
    String [] patient = new String [10];
    String [] doctor = new String [10];
    String [] surgery = new String [10];
    double [] cost = new double [10];

void start_system()
{
    String newLine;
    try
    {
        //define a file valiable for Buffered read
        BufferedReader Patient_Reports = new BufferedReader(new FileReader("patient.txt"));
        //read lines in file until there are no more lines in the file to read
        while ((newLine = Patient_Reports.readLine()) != null)
        {
            //there is a "," between each data item in each line
            StringTokenizer delimiter = new StringTokenizer(newLine,",");
            count=count+1;
            id[count] = Integer.parseInt(delimiter.nextToken());
            patient[count] =delimiter.nextToken();
            doctor[count] =delimiter.nextToken();
            surgery[count] =delimiter.nextToken();
            cost[count] = Double.parseDouble(delimiter.nextToken());
        }//while loop
        Patient_Reports.close();
    }//end try
    catch (IOException error)
    {
        //there was an error on the file writing
        System.out.println("Error on file read " + error);
    }//error on read
}//end start_system
//Method for Menu of Reports
String menu()
{
    String pnum;
    String Output =  "Reports" + "\n" +"1. All_Information_Report" + "\n" +
            "2. Surgeries_Doctor_Report" + "\n" +
            "3. Surgeries_Type_Report" + "\n" +
            "4. Doctor_Fees_Report" + "\n" +
            "5. Average_Fees_Report" + "\n" +
            "6. Exit" + "\n" +
            " " + "\n" +
            "Select a Report >";
    pnum = JOptionPane.showInputDialog(null,
            Output, "",JOptionPane.QUESTION_MESSAGE);
    return pnum;
}//end menu

/ *报告的占位符

//All_Information_Report Report containing all of the information

void All_Information_Report()
{
    System.out.println("All_Information_Report");
    for (i=0; i<=count; ++i)
    {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Surgeries_Doctor_Report Report on all surgeries of a specific doctor (prompt for the doctor)

void Surgeries_Doctor_Report()
{
    System.out.println("Surgeries_Doctor_Report");
    for (i=0; i<=count; ++i)
    {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Surgeries_Type_Report Report on all surgeries of a specific type(Prompt for the surgery type)

void Surgeries_Type_Report()
{
    System.out.println("Surgeries_Type_Report");
    for (i=0; i<=count; ++i)
    {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Doctor_Fees_Report Report on the total amount of fees paid to each doctor

void Doctor_Fees_Report()
{
    System.out.println("Doctor_Fees_Report");
    for (i=0; i<=count; ++i)
    {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Average_Fees_Report Report on the Average Fee

void Average_Fees_Report()
{
    System.out.println("Average_Fees_Report");
    for (i=0; i<=count; ++i)
    {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

* /     //将数据文件存储在数组中

void Exit()
{
    try
    {
        BufferedWriter Patient_Reports = new BufferedWriter(new FileWriter("patient.txt"));
        for (i=0; i<=count; ++i)
        {
            //put "," between each data item in the file
            Patient_Reports.write(id[i] + "," + patient[i] + "," + doctor[i]+ "," + surgery[i] + ","+ cost[i]+ ",");
            //write a new line in the file
            Patient_Reports.newLine();
        }//for loop
        Patient_Reports.close();
    }//end try
    catch (IOException error)
    {
        //there was an error on the write to file
        System.out.println("Error on file write " + error);
    }//end error
}//end class

} //结束exit_system`

1 个答案:

答案 0 :(得分:1)

首先,您的代码非常令人困惑,违反了几项OOP编程惯例。

第一个(也是最令人震惊的)问题是您的静态类Patient_Reports包含main方法但不使用此静态入口点来声明对象或前进。因此,您可以像这样重新格式化代码:

public class Patient_Reports {
    Patient_Reports fields...

    public Patient_Report() {
         //start work here in Constructor

    }

    Patient_Reports methods...

    public static void main(String[] args) {
        new Patient_Reports();
    }        

}

要解决您的主要问题,您可能想要在类中调用某种通用方法,如下所示:

 public static void writePatientReports(int[] id, String[] patient, 
      String[] doctor, String[] surgery, double[] cost) {

     BufferedWriter Student_file = new BufferedWriter(new FileWriter("patient.txt"));
     for (i=0; i<=count; ++i)
    {
        //put "," between each data item in the file
        Student_file.write(id[i] + "," + patient[i] + "," + doctor[i]+ "," + surgery[i] + ","+ cost[i]+ ",");
        //write a new line in the file
        Student_file.write("\n");
    }//for loop
    Student_file.close();
  }

您收到错误的原因是因为您没有像write()newLine()那样附加到Patient_Reports的方法。即使你有,你也会不必要地将Student_file声明为BufferedWriter对象。

此外,通过阅读Java Code Conventions

,您将大大提高您对Java和一般OOP的了解