【问题标题】:Java .split() not workingJava .split() 不工作
【发布时间】:2015-03-13 14:17:26
【问题描述】:

我正在尝试从文本文件中拆分一行。正在从库存系统导入文本文件,我不知道如何将其格式化为表格格式。我不能告诉你文件中有什么,但我会解释我在说什么。保密...

第 1 行:

name order sorder assemblyID desc date

123 123 123 1-2-3 123-456-789 12-3 1\2\3

第 2 行:

123 123 123 1-2-3 123-456-789 12 3 1\2\3

如果你能看到...描述列中有一个空格。这意味着它将分配到我数组的单独部分。第一个数组的大小为 7,但第二个数组的大小为 8。这就是我所拥有的。

public static void main(String[] args) throws IOException, ParseException {

    ArrayList < CustordData > list = new ArrayList < CustordData > ();
    CustordData cd = new CustordData();
    int[] array = new int[10];
    DateFormat format = new SimpleDateFormat("MM/dd/Y");

    try {
      String read = null;
      BufferedReader in = new BufferedReader(new FileReader("Custord.txt"));
      while ((read = in .readLine()) != null) {
        String[] splited = read.split("\\s+");
        cd.setCustName(splited[0]);
        cd.setPurchaseOrder(splited[1]);
        cd.setSalesOrder(splited[2]);
        cd.setAssemblyID(splited[4]);
        cd.setOrderDesc(splited[5]);
        cd.setKitDate(format.parse(splited[6]));
      }
      for (CustordData d: list) {
        System.out.println(d.getCustName() + d.getPurchaseOrder() + d.getSalesOrder() + d.getAssemblyID() + d.getOrderDesc() + d.getKitDate() + d.getShipDate() + d.getPricePer() + d.getTotal());
      }

    } catch (IOException e) {
      System.out.println("There was a problem: " + e);
    }

【问题讨论】:

  • 您必须手动解析它。您如何知道列描述中有一个空格?而不是其他专栏?分隔文件中未转义的分隔符不明确,程序无法读懂您的想法。
  • 正如鲍里斯所说,根据第二个示例,5、6 或 7 中的任何一个都可以有空间。您需要转义分隔符或想出一种方法来找出更好的空间。

标签: java object bufferedreader readline


【解决方案1】:

如果您确定只有一列会有额外的空格,并且它会始终是同一列,您仍然可以使用 split 但您会做一些事情像这样:

Let N be the index of the description column, 
Assign columns [1, N-1] to the data you need
Assign columns [N, TotalColumns - 1] to description
Assign column TotalColumns to date

类似这样的:

   public static void main(String[] args) {
   String noSpaces = "123 123 123 1-2-3 123-456-789 12-3 1\\2\\3";
   String withSpaces = "123 123 123 1-2-3 123-456-789 12 3 1\\2\\3";

   String[] splitNoSpaces = noSpaces.split("\\s+");
   printData(splitNoSpaces);

   String[] splitWithSpaces = withSpaces.split("\\s+");
   printData(splitWithSpaces);

}

private static void printData(String[] data)
{
    int totalColumns = data.length;

    System.out.println("Name: " + data[0]);
    System.out.println("Order: " + data[1]);
    System.out.println("SOrder: " + data[2]);
    System.out.println("AssemblyID: " + data[3]);
    System.out.print("Description: ");
    for(int i = 4; i < totalColumns - 2; i++)
    {
        System.out.print(data[i] + " ");
    }
    System.out.println();
    System.out.println("AssemblyID: " + data[totalColumns - 1]);
}

产量:

Name: 123
Order: 123
SOrder: 123
AssemblyID: 1-2-3
Description: 123-456-789 
AssemblyID: 1\2\3
Name: 123
Order: 123
SOrder: 123
AssemblyID: 1-2-3
Description: 123-456-789 12 
AssemblyID: 1\2\3

【讨论】:

    【解决方案2】:

    如果您知道唯一的问题在于描述,请测试数组的长度。然后将数组从索引 5 连接到数组长度 - 1。 KitDate 将是数组的最后一个字段。

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多