简体   繁体   English

如何从Java中的文本文件读取空集

[英]How to read an empty set from a text file in Java

I have 3 String fields per line within my text file. 我的文本文件中每行有3个String字段。 There are 4 lines in total. 共有4条线。 The first 2 fields ( field[0] and field[1] ) are already filled in but field 3 ( field[2] ) is yet to be generated so it shall remain empty. 前两个字段( field[0]field[1] )已经填写完毕,但尚未生成字段3( field[2] ),因此应保持为空。 Is there any way I can read in this text file line by line without getting a java.lang.ArrayIndexOutOfBoundsException: 1 error? 有什么方法可以逐行读取此文本文件而又不会出现java.lang.ArrayIndexOutOfBoundsException: 1错误? I have included my code used for reading in the file. 我已将用于读取的代码包含在文件中。

import java.io.*;
public class PassGen {
   public static void main(String args[]) throws Exception{
      BufferedReader inKb = new BufferedReader(new InputStreamReader(System.in));
      BufferedReader inF = new BufferedReader(new FileReader(new File("students.txt")));

      String line = inF.readLine();
      int cnt = 0;
      Student pupil[] = new Student[6];

      while(line != null) {
          String field[] = line.split("//s");
          pupil[cnt] = new Student(field[0], field[1], field[2]);
          cnt++;
          inF.readLine();
      }       
   }
}

You can simply add a check on the number of fields: 您可以简单地对字段数添加检查:

if(field.length > 2) {
   pupil[cnt] = new Student(field[0], field[1], field[2]);
} else {
   pupil[cnt] = new Student(field[0], field[1], null);
}

Alternatively, you can use the overloaded split method that takes a limit parameter and set that to -1 to include the empty field. 或者,您可以使用带有限制参数的重载split方法,并将其设置为-1以包括空字段。 From the documentation of String#split(String regex, int limit) : String#split(String regex, int limit)的文档中:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. limit参数控制应用图案的次数,因此会影响所得数组的长度。 If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n , and the array's last entry will contain all input beyond the last matched delimiter. 如果限制n大于零,则将最多应用n-1次该模式,该数组的长度将不大于n ,并且该数组的最后一个条目将包含除最后一个匹配的定界符之外的所有输入。 If n is non-positive then the pattern will be applied as many times as possible and the array can have any length . 如果n为非正数,则将尽可能多地应用该模式,并且数组可以具有任何长度 If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 如果n为零,则该模式将被尽可能多地应用,该数组可以具有任何长度,并且尾随的空字符串将被丢弃。

Note that you need to use \\\\s instead of //s for the whitespace regex (this needs to be corrected either way). 请注意,您需要使用\\\\s而不是//s作为空白正则表达式(这需要以任何一种方式进行更正)。

String field[] = line.split("\\s", -1);

I think you problem lies in the way you are managing your data, but you can have something like this to read from any array and not getting any exceptions: 我认为您的问题出在管理数据的方式上,但是您可以从任何数组中读取类似的内容,而不会出现任何异常:

  public static String getIfExists(final String[] values, final int position) {
    return (values != null) && (values.length > position) ? values[position] : null;
  }

Then you can fill every field like new Student(getIfExists(field, 0), getIfExists(field, 1), getIfExists(field, 2)); 然后,您可以填充每个字段,例如new Student(getIfExists(field, 0), getIfExists(field, 1), getIfExists(field, 2));

Of course you can optimize this a little bit more...but that would make the trick without having to think on how many fields you might get in the future or having a lot of if / case conditions. 当然,你可以优化该多一点点......但是,这将使的伎俩而不必考虑你可能有多少个字段在未来获得或有很多的if / case的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM