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

Output a part of a String if the beginning of the String starts with a keyword

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

我用filereader读取一个文件的内容没问题,也输出到另一个文件中,在String中给定的地方输出一个子串。 (是的,对于了解 EDI 的人来说,您知道它是一个 VDA 文件)

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

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

这是IM读到的输入条件,每一行都是128个字符,长̶b̶̶u̶̶t̶̶t̶̶t̶̶t̶̶t̶̶h̶̶a̶̶a̶̶h̶̶a̶̶s̶̶b̶̶e̶̶e̶̶e̶̶e̶̶t̶̶e̶̶t̶̶e̶̶t̶̶r̶̶im̶̶m̶̶e̶̶m̶̶e̶̶m̶̶im̶̶m̶̶im̶̶nn̶̶trim̶̶nn̶̶tim̶̶

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);