如何读取txt文件的每一列,并将它们放在单独的数组中? [英] How do I read each column of a txt file, and place them in separate arrays?

查看:106
本文介绍了如何读取txt文件的每一列,并将它们放在单独的数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行编程分配,基本上我需要从txt文件中读取并对不同数组中的所有内容进行排序,这样我就可以整齐地显示cmd提示中的所有内容,并能够删除内容。

I'm doing a Programming Assignment and basically I need to read from a txt file and sort everything in there in different arrays allowing me to display everything in the cmd prompt neatly and be able to delete stuff.

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(员工,医生)。第二列是employeeName。第三列是employeeNumber,其中一些列有第四列(如果它是一个数字,它是患者的数量.Y用于扫描或接听电话)

First column needs to be the employeeRole (employee, doctor). The second column being the employeeName. Third column being the employeeNumber and some of them have have a fourth column (if it's a number it's number of patients. Y is for like sweeping, or answering calls)

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

So my thought process was put each column into it's own array and then writing it out that way. I was able to put each row into its own array with

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]);
}

但我无法弄清楚该做些什么。然后进行拆分会将数组放入数组中?这意味着我会将每一行放入一个数组,然后将每个单词放入一个数组中?

But I couldn't figure out what to do from there. Doing a split would then put an array into an array? Which means I would put each line into an array and then each word into an array?

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

So I tried something else, anything with the length not equal to 1 would be the employeeNum, but then they there were the N's and Y's and the number of pateints.

    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]);
}

我只是不确定我是否采取了正确的方式?如果我让它变得比实际更困难?或者,如果有更简单的方法来做到这一点。但是一切都完成后,输出应该基本上可以说

I'm just not sure if I'm going the right way about it? If I'm making it more difficult than it really is? Or if there's an easier way to do this. But after everything is done, the output should be able to basically say

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

任何帮助非常感谢,谢谢!

Any help would be greatly appreciated, thanks!

推荐答案

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

You don't have to open 4 streams in order to read the file (I guess you wanted to open "one per column" but you shouldn't do it).

其次,您可以在空格()上拆分字符串,它将为您提供完全符合您要求的列(对于每一行)。

Second, you can split the string on spaces (" ") which will provide you the columns (for every line separately) exactly like you want.

代码示例:

    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();
        }
    }

这篇关于如何读取txt文件的每一列,并将它们放在单独的数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆