【问题标题】:How to further split information that was read in from a txt file using the split method in a while loop?如何在 while 循环中使用 split 方法进一步拆分从 txt 文件中读取的信息?
【发布时间】:2014-08-31 10:28:30
【问题描述】:

例如,文本文件中的一行是:Richard,RL#17

我在一个while循环中拆分数据,我可以成功得到两条独立的信息。

现在我该怎么做才能进一步拆分字段 [0] - 字符串 - 以便我可以将“Richard”和“RL”分开。我必须再次拆分,这次是“,”。

  File inputFile = new File ("Personnel.txt");
  FileReader in = new FileReader (inputFile);
  BufferedReader inFromFile = new BufferedReader (in);

  String line = inFromFile.readLine ();
  int cnt = 0;

  while (line != null)
  {
     String [] field = line.split ("#");
     int age = Integer.parseInt (field [1]);
     workers [cnt] = new Personnel (field [0], age);
     cnt++;
     line = inFromFile.readLine();
  }
  inFromFile.close();

  for (int i = 0; i < cnt; i ++)
  {
     System.out.println ();
     System.out.println (workers [i]);
  }

所以我可以使用配对对象类成功读取数据并显示它。现在,我想知道我会进一步将具有“Richard,RL#17”的名字分成两个独立的过去。可以再用split方法吗,如果可以,怎么用?

我已尝试构建另一个 while 循环,但我不确定如何引用字段 [0] - 在本例中为文本文件中的名称 - 并从那里拆分它。我尝试做另一个程序,但没有成功。另一个程序如下:

     File inputFile = new File ("placeHolder.txt");
     FileReader inFile = new FileReader (inputFile);

     placeHolder[] lotsOfText = new placeHolder[10];

     int cnt = 0;

     for(int i = 0; i < lotsOfText.length; i++)
     {
        String line = inFile.readLine();
        while(line != null)
        {
         String[] field = line.split(",");
         field = line.split("#");
         lotsOfText[cnt] = new placeHolder(field[0],field[1],field[2],field[3]);


         cnt++;
        }   
     }  

     System.out.println(field[0] + " " + field[1] + " " + field[2] + " " + field[3]);

   }

【问题讨论】:

  • 嗨,理查德。首先,我们在 Stack Overflow 上并没有真正做“紧急”;问题和答案有望在多年内提供有用的信息,很可能在参与交流的人早已离开该网站很久之后。其次,我们希望提问者描述他们尝试过的内容以及结果与预期行为有何不同。请edit在您的问题中包含此类信息。
  • 请添加您的代码,以便我们(希望)了解您对 field[0] 的含义以及“两个单独的部分”是什么意思。目前,很难猜测您已经拥有什么。
  • 抱歉,我会进行推荐的修改。这是我关于堆栈溢出的第一个问题,所以我对规则和规定还是很陌生。
  • 我们都是初学者。拨打Tour,然后查看Help Center,以更深入地了解该网站的运作方式以及我们对用户的期望。最重要的可能是要记住帖子通常应该是独立的(补充信息的外部参考很好,但不应该要求他们理解帖子),并且投票是关于帖子的,而不是关于你的。
  • 这个问题现在好多了,但我仍然觉得它缺少对您为自己解决问题而尝试做的事情的解释。

标签: java split filereader


【解决方案1】:

我认为这就是您想要实现的目标。

添加到 Manjunath Anand 的回复中: 如果你想从文件中的一行中获取 3 个字符串,“Richard”,“RL”,“17”,然后存储到一个对象中并打印它,你可以这样做:

编辑:添加了一些异常处理并测试了代码:

public class Testing {

class Personnel {
    String name;
    String initials;
    int age;
    Personnel(String name, String initials, int age) {
        this.name = name;
        this.initials = initials;
        this.age = age;
    }
}

void readData() {
    File inputFile = new File ("Personnel.txt");
    FileReader in = null;
    BufferedReader inFromFile = null;
    ArrayList<Personnel> data = null;
    try {
        in = new FileReader (inputFile);
        inFromFile = new BufferedReader (in);

        // list that will hold all personnel objects
        data = new ArrayList<Personnel>();
        String line = inFromFile.readLine ();

        while (line != null) {
            String [] field = line.split("([,#])");
            String name = field[0]; // field[0] = "Richard"
            String initials = field[1]; // field[1] = "RL"
            int age = Integer.valueOf(field[2]);// field[2] = "17";
            data.add(new Personnel (name, initials, age));
            line = inFromFile.readLine();
        }
        inFromFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    for(Personnel p : data) {
        System.out.println("Name:\t\t" + p.name);
        System.out.println("Initials:\t" + p.initials);
        System.out.println("Age:\t\t" + p.age);
        System.out.println();
    }
}

public static void main(String[] args) {
    Testing test = new Testing();
    test.readData();
}

}

【讨论】:

  • 是的,看起来很棒。现在我将如何显示这两个?显示循环仍然在输出屏幕上显示:Richard,RL。
  • 它打印“Richard,RL”,因为你正在这样做:workers [cnt] = new Personnel (field [0], age);您仍在将 field[0] 添加到 Personnel 对象中,而不是 nameAndInitials[0] 和 nameAndInitials[1]。还有许多其他方法可以实现您想要的。我使用了 ArrayList,因为如果您使用数组,您必须提前知道您将拥有多少个项目。试试我回答中的代码,看看是否是你需要的
  • 你能解释一下“data.add”吗?编译器在该行指出一个错误。
  • TestPersonnel.java:22:找不到符号符号:构造函数 Personnel(java.lang.String,java.lang.String,int) 位置:类 Personnel data.add(new Personnel (nameAndInitials[0 ], nameAndInitials[1], age)); ^ TestPersonnel.java:22:找不到符号符号:变量数据位置:类 TestPersonnel data.add(new Personnel (nameAndInitials[0], nameAndInitials[1], age)); ^ 2 个错误
  • 出现同样的错误,“找不到符号”。
【解决方案2】:

试试下面的代码:-

String str1 = "Richard,RL#17";
String[] arr = str1.split("([,#])");

它会给你一个数组中的所有镜头

编辑:

    File inputFile = new File ("Personnel.txt");
    FileReader in = new FileReader (inputFile);
    BufferedReader inFromFile = new BufferedReader (in);
    // Create a scanner object which has convenient methods to read from file reader.
    Scanner s = new Scanner(inFromFile); 
    String [] field;
    String personLine;
    // Check if there is any data in the file
    while(s.hasNextLine()) {
        // read the entire line form the scanner of the file
        personLine = s.nextLine().trim();
        field = personLine.split("([,#])"); // you can put any number of delimiters within the square braces which seperate the data for person stored in the file
        // rest of your code logic
    } // keep reading till you finish all data

【讨论】:

  • 我需要从文本文件中读取信息。将有许多列出的名称遵循逗号后具有首字母的相同模式,我需要一个循环以满足进一步拆分。
  • @RichardLuiz 请检查编辑后的答案是否符合您的需求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 2021-11-18
  • 2021-04-05
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
相关资源
最近更新 更多