简体   繁体   English

将随机行数的字符串保存到单行数组JAVA

[英]Save String with random number of lines to single line array JAVA

So here is the method which is reading from the file, it then splits the information by the @ sign. 因此,这里是从文件读取的方法,然后用@符号分割信息。 which is where a new month begins in the text file 这是文本文件中新的月份开始的地方

public static String readPurchaseOrder(Scanner sc) {

    final String DELIMITER = "@";
    try {           
        while (sc.hasNext()) {
            sc.useDelimiter(DELIMITER);
            String data = sc.next();               
            return data;
        }

    } catch (Exception e) {
        System.out.println(e);
    }
    sc.close(); 
    return null;
}

The text file contains information shown below up to the 12th month 文本文件包含以下显示的信息,直到第12个月

  04/01/12#PNW-1234#PA/1234#10
    15/01/12#BSE-5566#bT/4674#5@
    08/02/12#PNE-3456#Xk/8536#1@
    07/03/12#PEA-4567#ZR/7413#3
    09/03/12#ESE-6329#HY/7195#30@
    03/04/12#ESE-5577#LR/4992#12
    23/04/12#PNW-1235#HY/7195#2@
    09/05/12#ESE-6329#PV/5732#6
    25/05/12#BSE-5566#PV/5732#10@
    08/06/12#PNE-3457#kD/9767#1
    31/06/12#EMI-6329#ZR/7413#10@
    03/07/12#EMI-6329#PV/5732#12
    25/07/12#BSE-5566#bT/4674#5@

I am using this to output the information from the file split by the @ 我正在用它来输出由@分割的文件中的信息

 for (int i = 0; i <12; i ++){
          String str[] = InputFileData.readPurchaseOrder(sC).split("\\n");
          for(String s : str){
              System.out.println(s);
          }

It outputs the data like this 它像这样输出数据

04/01/12#PNW-1234#PA/1234#10
15/01/12#BSE-5566#bT/4674#5

08/02/12#PNE-3456#Xk/8536#1

07/03/12#PEA-4567#ZR/7413#3
09/03/12#ESE-6329#HY/7195#30

03/04/12#ESE-5577#LR/4992#12
23/04/12#PNW-1235#HY/7195#2

09/05/12#ESE-6329#PV/5732#6
25/05/12#BSE-5566#PV/5732#10

I want to store each individual line in an array, so I can then further split up the line to its each respective variables 我想将每一行存储在一个数组中,因此我可以进一步将该行拆分为每个变量

If you would like to collect the results in an array, one line per array element, the easiest way to do it is to use a list (since you don't know in advance the number of lines), and then convert it to an array. 如果您想将结果收集到一个数组中,每个数组元素一行,那么最简单的方法是使用列表(因为您事先不知道行数),然后将其转换为数组。 The size of an array has to be declared in advance, so you want to use a more flexible data structure if you don't know how big it's going to be. 数组的大小必须事先声明,因此如果您不知道数组的大小,则希望使用更灵活的数据结构。

public static String[] readPurchaseOrder(Scanner sc) {
    final String DELIMITER = "@";
        List<String> results = new ArrayList<>();
        try {           
            while (sc.hasNext()) {
                sc.useDelimiter(DELIMITER);
                String data = sc.next();               
                results.add(data);  // add the line to the list
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        sc.close(); 
        // convert the list to an array and return it.
        return results.toArray(new String[results.size()]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM