如果字符串的开头以关键字开头,则输出字符串的一部分



所以,我开始学习Java和它的超级有趣和令人困惑的同时。

我使用filereader读取文件的内容没有问题,并且还将其输出到另一个文件中,并在字符串的给定位置上输出子字符串。(是的,对于那些知道EDI的人,你知道它是一个VDA文件)

但是,我想读取一行的开头,然后根据在行中的位置输出几个子字符串..

所期望的输出:找到55901,然后Stringname = substringname(5,12) Stringname = substringname(13,20)等

这是我阅读inputString,每条线长128字符b̶̶̶̶u t̶̶̶̶̶̶我t̶̶̶̶̶h̶̶̶一s̶̶̶̶̶̶b e̶̶̶̶e n̶̶̶̶̶̶t r̶̶̶我̶̶m̶̶̶e d̶̶̶̶̶̶我n̶̶̶̶̶t̶̶̶h e̶̶̶̶̶̶p o̶̶̶̶s t̶̶̶̶̶̶一u̶̶̶t̶̶̶o m̶̶̶̶一t̶̶̶我̶̶c̶̶̶一l̶̶̶l̶̶̶y:

55101BUYX     SELLLX    0022200223210924                                                                                        
55201XQ 000897350210924  PARTNUMERXXX                            ZZ           S000000V            0000000000                  
55301000000000000000000000000000000000000000000I                                                                               
554012109240000000000462 2109270000000000000 2109280000000000000 2109290000000000000 2109300000000000000 2110010000000000000   
554012110040000000000000 2110050000000000000 2110060000000000000 2110070000000000000 0000000000000000000 0000000000000000000   
55701                                                                                                    JP010 ASDFGHJK         
55901_output000000200000020000004000000000000020000001

这是我目前所拥有的:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.lang.*;
public class Main {
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 buyer = "";
//Scan next line if exists
try {
while (scan.hasNextLine()) {
input = input.concat(scan.nextLine() + "n");
}
}
catch (Exception e){
System.out.println("Error in the code");
}
scan.close();
buyer = input.substring(5, 13);
System.out.println("Buyer "+buyer);
}
}

是。我认为你应该在连接扫描器到输入的下一行之前这样做。

try {
while (scan.hasNextLine()) {
String nextLine = scan.nextLine();
//now find 55901
String keyword = "55901"
if(nextLine.startsWith(keyword){
//go ahead and extract the content you want from nextLine
buyer = nextLine.substring(5, 13);
}
input = input.concat(nextLine + "n");
}
}

非常感谢你,这就像一个魅力,但我遇到了另一个问题,我似乎找不到答案。我用这个方法提取12个子字符串,但最后2个字符串总是空的。另外,当我添加一个else if来查找下一个55401行,它不包含任何值。

从下面的代码我得到以下,字符串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 + shipDate1)){
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);

最新更新