【问题标题】:Find string inside of the text file. Then getting the following line and need to Split by Index of and substring在文本文件中查找字符串。然后得到以下行,需要按索引和子字符串拆分
【发布时间】:2020-03-25 15:32:29
【问题描述】:

在文本文件中查找字符串。然后得到下面一行,用indexOf()substring()分割。

import java.util.*; 
import java.io.*;

public class FileReadTest {

    public static void main(String[] args) throws IOException {
        File f = new File("a.dat");
        Scanner fin = new Scanner(f);
        String airportcode = "HOI";

        while (fin.hasNextLine()) {
            String line = fin.nextLine();
            int firstindex = line.indexOf(airportcode);

            if (firstindex > 0) {
                int Code = line.indexOf("|");
                int Country = line.lastIndexOf("|",Code);
                int State = line.indexOf("|", Country);
                int City = line.indexOf("|", State);
                int Airport = line.indexOf("|", City);
                System.out.println(Code);
                System.out.println(Country);
                System.out.println(State);
                System.out.println(City);
                System.out.println(Airport);
                System.out.println(line.substring(0, Code));
                break;
            }
        }

        fin.close();
    }
}

1 sout 看起来像这样: French Polynesia|HOI|Hao|Tuamotos|Hao Airport 我只需要使用indexOf()substring(), 但我需要这样:

French Polynesia
HOI
Hao
Tuamotos
Hao Airport

我该怎么办?

【问题讨论】:

  • 您是否总是为文件的每一行设置相同数量的字段并始终以| 分隔?
  • 法属波利尼西亚|HOI|Hao|Tuamotos|Hao Airport 我需要分隔“|” @dariosicily

标签: java string substring java.util.scanner indexof


【解决方案1】:

从假设您始终具有相同数量的字段开始,在您的情况下,5 由字符 | 分隔,您可以在不使用 String split 方法的情况下解决问题,而只需使用 indexOfsubstring,如下所示:

String s = "French Polynesia|HOI|Hao|Tuamotos|Hao Airport";
for (int i = 0; i < 4; ++i) {
    int endIndex = s.indexOf("|");
    System.out.println(s.substring(0, endIndex));
    s = s.substring(endIndex + 1);
}
System.out.println(s);

代码将打印所有可以分配给不同变量的字段。

【讨论】:

  • 这是工作,但最后的数据没有出来
  • @sandunwijerathneJerry 忘记了一行,字符串s包含最后的数据
【解决方案2】:

假设:

  • 文件内容具有以下结构的行:French Polynesia|HOI|Hao|Tuamotos|Hao Airport
  • 您只需要打印那些包含"HOI" 字符串的行
  • 您只能使用indexOfsubstring

这里是适合您的代码 sn-p(文件 a.dat 位于 resources 文件夹中):

package example;

import java.util.*; // for Scanner
import java.io.*; // for File and IOException

public class FileReadTest {

    public static void main(String[] args) throws IOException {
        File f = new File(
            Objects.requireNonNull(FileReadTest.class.getClassLoader().getResource("a.dat")).getFile()
        );
        Scanner fin = new Scanner(f);
        String airportcode = "HOI";
        while (fin.hasNextLine()) {
            String line = fin.nextLine();
            if (line.indexOf(airportcode) != -1) {
                int firstindex;
                while ((firstindex = line.indexOf("|")) != -1) {
                    System.out.println(line.substring(0, firstindex));
                    line = line.substring(firstindex + 1);
                }
                System.out.println(line); // last data 
            }
        }
    }
}

【讨论】:

  • 谢谢,@fabasoad 你的回答也正常
猜你喜欢
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 2023-03-31
相关资源
最近更新 更多