[英]Java .split() not working
I am trying to split a line from a text file. 我正在尝试从文本文件中拆分一行。 The text file is being imported from an inventory system and I don't know how to make it formatted with a tabular format.
该文本文件是从库存系统中导入的,我不知道如何使用表格格式对其进行格式化。 I can't tell you what is in the file but I will explain what I'm talking about.
我无法告诉您文件中包含的内容,但我会解释我在说什么。 Confidentiality...
保密...
Line 1: 第1行:
name order sorder assemblyID desc date 名称顺序顺序AssemblyID描述日期
123 123 123 1-2-3 123-456-789 12-3 1\\2\\3 123123123 1-2-3 123-456-789 12-3 1 \\ 2 \\ 3
Line 2: 第2行:
123 123 123 1-2-3 123-456-789 12 3 1\\2\\3 123123123 1-2-3 123-456-789 12 3 1 \\ 2 \\ 3
If you can see... The description column has a space in it. 如果您看到...说明栏内有一个空格。 Which means it will allocate that into separate part of my array.
这意味着它将把它分配到我数组的单独部分。 First array is of size 7 but the second array will be 8. Here is what I have.
第一个数组的大小为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);
}
If you are certain that only one column will have extra spaces, and that it will be always the same column , you could still use split but you would do something like so: 如果确定只有一列具有多余的空格,并且始终是同一列 ,则仍然可以使用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
Something like so: 像这样:
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]);
}
Yields: 产量:
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
If you know that only problem is in description then test lenght of array. 如果您只知道描述中存在问题,则测试数组长度。 Then join array from index 5 to array lenght - 1. KitDate will be last field of array.
然后将数组从索引5连接到数组长度-1。KitDate将是数组的最后一个字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.