繁体   English   中英

如何检查文本文件中是否包含单个字符串或两个字符串?

[英]How to check if a text file contain a single string or two strings?

可以说我有一个文本文件,每行有两个字符串:

New York 52.523405 13.4114
San Antonio 41.387917 2.169919
Los Angeles 51.050991 13.733634

这是我的代码,将字符串从行中分离出来:

for (int i = 0; i < noOfStores;i++){
    nextLine = console.readLine();
    nextLine = nextLine.trim();
    String temp[] = nextLine.split(" ");
    String Word = temp[0] + " " + temp[1];
    storeNames[i] = firstWord;
    latitudes[i] = Double.parseDouble(temp[2]);
    longitudes[i] = Double.parseDouble(temp[3]);
}

但是,如果一个文本文件在每一行中仅包含一个字符串,如下所示:

Berlin 52.523405 13.4114
Barcelona 41.387917 2.169919
Dresden 51.050991 13.733634

读取文本文件时如何检查一个文本文件包含一两个字符串?

使用split(" ") ,获取返回的数组长度,然后将数组中最后两个String数组项( length - 1length - 2项)解析为双精度,然后迭代最后一个String项。两个项目并将它们组合为城市字符串。 就像是,

for (int i = 0; i < noOfStores;i++){
    nextLine = console.readLine();
    nextLine = nextLine.trim();
    String temp[] = nextLine.split(" ");
    int length = temp.length;
    if (length < 3) {
        // output is not as expected; throw some type of exception here.
    }
    latitudes[i] = Double.parseDouble(temp[length - 2]);
    longitudes[i] = Double.parseDouble(temp[length - 1]);

    // this should handle city names with 1, 2 or any number of tokens
    StringBuilder wordSb = new StringBuilder();
    for (int j = 0; j < length - 2; j++) {
       wordSb.append(temp[j]);
       if (j != length - 3) {
          wordSb.append(" ");
       }
    }
    storeNames[i] = wordSb.toString();
}

使用正则表达式。

String testData = "New York 52.523405 13.4114\n" +
                  "San Antonio 41.387917 2.169919\n" +
                  "Los Angeles 51.050991 13.733634\n" +
                  "Berlin 52.523405 13.4114\n" +
                  "Barcelona 41.387917 2.169919\n" +
                  "Dresden 51.050991 13.733634";

Pattern p = Pattern.compile("\\s*(.*?)\\s+(-?[0-9.]+)\\s+(-?[0-9.]+)\\s*");
try (BufferedReader in = new BufferedReader(new StringReader(testData))) {
    String line;
    while ((line = in.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (! m.matches())
            throw new IllegalArgumentException("Bad data: " + line);
        String storeName = m.group(1);
        double latitude = Double.parseDouble(m.group(2));
        double longitude = Double.parseDouble(m.group(3));
        System.out.printf("Store '%s' is at %f, %f%n", storeName, latitude, longitude);
    }
}

产量

Store 'New York' is at 52.523405, 13.411400
Store 'San Antonio' is at 41.387917, 2.169919
Store 'Los Angeles' is at 51.050991, 13.733634
Store 'Berlin' is at 52.523405, 13.411400
Store 'Barcelona' is at 41.387917, 2.169919
Store 'Dresden' is at 51.050991, 13.733634

最适合您任务的工具是正则表达式。 您可以确定两个数字不包含任何空格,可以将它们定义为"\\S+" ,而其他任何内容都可以通过name的pattern进行匹配。

这使您可以在name部分中包含任意数量的单词(以及字面意义上的任何其他内容),同时允许内部使用任何格式的数字(例如科学计数法),只要它们内部没有空格。

String[] lines = new String[]{
        "New York 52.523405 13.4114",
        "San Antonio 41.387917 2.169919",
        "Los Angeles 51.050991 13.733634",
        "Berlin 52.523405 13.4114",
        "Barcelona 41.387917 2.169919",
        "Dresden 51.050991 13.733634",
        "Some scientific notation 1E-4 13.733634"
};

Pattern pattern = Pattern.compile("(.*)\\s+(\\S+)\\s+(\\S+)");

for (String line : lines) {

    Matcher matcher = pattern.matcher(line);
    if (matcher.matches()) {
        String name = matcher.group(1);
        double latitude = Double.parseDouble(matcher.group(2));
        double longitude = Double.parseDouble(matcher.group(3));

        System.out.printf("'%s', %.4f %.4f\n", name, latitude, longitude);
    }
}

结果:

'New York', 52.5234 13.4114
'San Antonio', 41.3879 2.1699
'Los Angeles', 51.0510 13.7336
'Berlin', 52.5234 13.4114
'Barcelona', 41.3879 2.1699
'Dresden', 51.0510 13.7336
'Some scientific notation', 0.0001 13.7336

暂无
暂无

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

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