【发布时间】:2021-11-19 20:44:26
【问题描述】:
我是 Java 新手,所以如果我问一些明显的问题,请原谅我。我知道你可能有更好的事情要做,但如果有人能指出我正确的方向,我会很感激。
我提取了 14 个子字符串,但最后 4 个字符串始终为空。 而且我不确定如何阅读以 55401 开头的下一行。 我可以如何提取任何子字符串有隐藏的限制吗?
我查看的文件:
- 55101BUYX SELLLX 0022200223210924
- 55201XQ 000897350210924 PARTNUMERXXX ZZ S000000V 0000000000
- 55301000000000000000000000000000000000000000000I
- 554012109240000000000462 2109270000000000000 2109280000000000000 2109290000000000000 2109300000000000000 2100010li>00000
- 554012110040000000000000 2110050000000000000 2110060000000000000 2110070000000000000 0000000000000000000000000000000000000
- 55701 JP010 ASDFGHJK
- 55901_output000000200000020000004000000000000020000001
从下面的代码我得到以下信息,字符串 6 和 7 没有得到值。
- 船舶 210924 |数量 000000462
- 船舶 210927 |数量 000000000
- 船舶 210928 |数量 000000000
- 船舶 210929 |数量 000000000
- 船舶 210930 |数量 000000000
- 船舶 |数量
- 船舶 |数量
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Mail\\Desktop\\VDA Reader\\VDA.txt");
Scanner scan = new Scanner(file);
String input = "";
String shipDate1 = "";
String qty1 = "";
String shipDate2 = "";
String qty2 = "";
String shipDate3 = "";
String qty3 = "";
String shipDate4 = "";
String qty4 = "";
String shipDate5 = "";
String qty5 = "";
String shipDate6 = "";
String qty6 = "";
String shipDate7 = "";
String qty7 = "";
String callOffs = "55401";
//Scan next line if exists
try {
while (scan.hasNextLine()) {
String outPut = scan.nextLine();
if(outPut.startsWith(callOffs)){
shipDate1 = outPut.substring(5, 11);
qty1 = outPut.substring(15, 24);
shipDate2 = outPut.substring(25, 31);
qty2 = outPut.substring(35, 44);
shipDate3 = outPut.substring(45, 51);
qty3 = outPut.substring(55, 64);
shipDate4 = outPut.substring(65, 71);
qty4 = outPut.substring(75, 84);
shipDate5 = outPut.substring(85, 91);
qty5 = outPut.substring(95, 104);
shipDate6 = outPut.substring(105, 101);
qty6 = outPut.substring(115, 124);
}
if (outPut.startsWith(callOffs)){
shipDate7 = outPut.substring(5, 11);
qty7 = outPut.substring(15, 24);
}
input = input.concat(outPut + "\n");
}
}
catch (Exception e){
}
scan.close();
System.out.println("Ship "+shipDate1 + " | QTY " + qty1);
System.out.println("Ship "+shipDate2 + " | QTY " + qty2);
System.out.println("Ship "+shipDate3 + " | QTY " + qty3);
System.out.println("Ship "+shipDate4 + " | QTY " + qty4);
System.out.println("Ship "+shipDate5 + " | QTY " + qty5);
System.out.println("Ship "+shipDate6 + " | QTY " + qty6);
System.out.println("Ship "+shipDate7 + " | QTY " + qty7);
【问题讨论】:
-
您的 cod 永远不会填写
shipDate6、qty6、shipDate7和qty7,因为在线shipDate6 = outPut.substring(105, 101);substring()方法会抛出IndexOutOfBoundsException,因为结束索引 ( 101) 小于起始索引 105。您永远不会看到此异常的任何痕迹,因为您的代码有助于捕获所有异常并忽略它们。
标签: java substring java.util.scanner