2

我正在做一个编程作业,基本上我需要从一个 txt 文件中读取并将其中的所有内容排序在不同的数组中,这样我就可以在 cmd 提示符下整齐地显示所有内容并能够删除内容。

h Vito 123
d Michael 234 Heart
s Vincent 345 Brain Y
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
n Vicky 911 7

第一列需要是employeeRole(员工、医生)。第二列是员工姓名。第三列是员工编号,其中一些有第四列(如果是数字,则表示患者人数。Y 代表扫地或接听电话)

所以我的思考过程是将每一列放入它自己的数组中,然后以这种方式写出来。我能够将每一行放入自己的数组中

public class ReadingFile {
 // String test;
  // char[] employeeRole = new char[9];
   String[] employeeRole = new String[9];
   String[] employeeName = new String[9], specialty;
   String[] wholeLine = new String[9];
  // String word;

   int[] employeeNum = new int[9];
   int r, n, l, num;
   public void Reader()
    {
    Scanner inputStream = null;
    Scanner inputStream2 = null;
    Scanner inputStream4 = null;
    try
    {
        BufferedReader inputStream3 =
                    new BufferedReader(new FileReader("data.txt"));
        inputStream = new Scanner(new FileInputStream("data.txt"));
        inputStream =
                    new Scanner(new FileInputStream("data.txt"));
        inputStream2 =
                    new Scanner(new FileInputStream("data.txt"));
                    inputStream4 =
                    new Scanner(new FileInputStream("data.txt"));
                    System.out.println("Yeah");

    }
    catch(FileNotFoundException e){
        System.out.println("File Not found");
        System.exit(1);
    }
for (l=0; l<9; l++)
{
    wholeLine[l] = inputStream2.nextLine();

    System.out.println(wholeLine[l]);
}

但我不知道从那里做什么。然后进行拆分会将数组放入数组中?这意味着我会将每一行放入一个数组中,然后将每个单词放入一个数组中?

所以我尝试了别的东西,长度不等于 1 的任何东西都是employeeNum,但是它们是 N's 和 Y's 以及专利的数量。

    for(r=0; r<9; r++) //role
    {
       String next = inputStream4.next();
       while( next.length() != 1)
       {
           next = inputStream4.next();
       }

        employeeRole[r] = next;

       System.out.println(employeeRole[r]);
    }

我也试过

for (r=0; r<9; r++)
{
    employeeRole[r] = wholeLine[r].substring(wholeLine[r].indexOf(1));
    //inputStream.nextLine();
    System.out.println(employeeRole[r]);
}

我只是不确定我是否走对了路?如果我让它变得比实际上更困难?或者,如果有更简单的方法可以做到这一点。但是一切搞定之后,输出应该基本可以说

Doctors: 2
Name: Michael  Employee Number: 234 Specialty: Heart
Name: Nicos     Employee Number: 891 Specialty: Bone

任何帮助将不胜感激,谢谢!

4

1 回答 1

6

您不必打开 4 个流来读取文件(我猜您想打开“每列一个”,但您不应该这样做)。

其次,您可以在空格(“”)上拆分字符串,这将为您提供完全符合您需要的列(分别为每一行)。

代码示例:

    BufferedReader br = null;
    String[] characters = new String[1024];//just an example - you have to initialize it to be big enough to hold all the lines!

    try {

        String sCurrentLine;
        br = new BufferedReader(new FileReader("data.txt"));

        int i=0;
        while ((sCurrentLine = br.readLine()) != null) {
            String[] arr = sCurrentLine.split(" ");
            //for the first line it'll print
            System.out.println("arr[0] = " + arr[0]); // h
            System.out.println("arr[1] = " + arr[1]); // Vito
            System.out.println("arr[2] = " + arr[2]); // 123
            if(arr.length == 4){
                System.out.println("arr[3] = " + arr[3]);
            }

            //Now if you want to enter them into separate arrays
            characters[i] = arr[0];
            // and you can do the same with
            // names[1] = arr[1]
            //etc
            i++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
于 2013-09-22T23:55:55.590 回答