【问题标题】:Output a part of a String if the beginning of the String starts with a keyword如果字符串的开头以关键字开头,则输出字符串的一部分
【发布时间】:2021-11-18 12:32:23
【问题描述】:

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

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

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

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

这是我读到的Inputstring,每一行都是128 Chars Long̶̶̶̶̶̶̶̶̶̶̶̶̶̶t̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶:

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

    }
}

【问题讨论】:

  • 您的问题不清楚。你想以某种方式分割你的线条吗?您是否在整个文件中寻找特定的子字符串?您已经拥有的代码有什么作用?它应该怎么做?

标签: java substring filereader keyword


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    非常感谢,这就像一个魅力,但我遇到了另一个我似乎无法找到答案的问题。我用这种方法提取了 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2014-12-02
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多