简体   繁体   English

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

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

Lets say I have a text file which have two strings in each line: 可以说我有一个文本文件,每行有两个字符串:

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

and this is my code to split the string out from the line: 这是我的代码,将字符串从行中分离出来:

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

but what if a text file contain only one string in each line like this: 但是,如果一个文本文件在每一行中仅包含一个字符串,如下所示:

Berlin 52.523405 13.4114
Barcelona 41.387917 2.169919
Dresden 51.050991 13.733634

How can I check whether a text file contain one or two string when reading it? 读取文本文件时如何检查一个文本文件包含一两个字符串?

Use split(" ") , get the returned arrays length, and then parse the last two String array items in the array, items length - 1 and length - 2 , as doubles, and then iterate through the remaining String items prior to the last two items and combine them as the City String. 使用split(" ") ,获取返回的数组长度,然后将数组中最后两个String数组项( length - 1length - 2项)解析为双精度,然后迭代最后一个String项。两个项目并将它们组合为城市字符串。 Something like, 就像是,

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

Use a regular expression. 使用正则表达式。

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

Output 产量

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

The most suitable tool for you task are regexps. 最适合您任务的工具是正则表达式。 As you can be sure that two of your numbers don't contain any spaces you can define them as "\\S+" , leaving anything else to be matched by pattern for name . 您可以确定两个数字不包含任何空格,可以将它们定义为"\\S+" ,而其他任何内容都可以通过name的pattern进行匹配。

This allows you to have any number of words (and literally anything else) in name part, in the same time allowing to have numbers in any format (like scientific notation) as long as they don't have spaces inside. 这使您可以在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);
    }
}

Result: 结果:

'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.

相关问题 如何检查两个EditText是否包含文本 - How to check if two EditText contain text 如何检查文件夹中文件名中的元素是否在字符串数组列表中包含字符串名? - How to check if elements in a file names in a folder contain string names in an arraylist of strings? 如何通过读取文本文件替换 java 中两个字符串之间的字符串 - how to replace string between two strings in java by reading a text file 检查未修剪字符串列表是否包含修剪字符串 - Check if List of untrimmed Strings contain trimmed String Junit如何检查字符串是否等于两个字符串之一? - Junit how to check if string is equal to one of two strings? 检查两个字符串是否包含相同顺序的相同字符 - Check whether two strings contain same characters in same order 比较两个包含日期的字符串,以检查一个字符串是否在另一个字符串之前或之后 - Compare two strings that contain a date to check if one is before or after the other 如何区分一个字符串中的两个字符串?(如何防止纯文本注入) - How to distinguish two strings in a String?(How to prevent plain text injection) 如何检查文本文件中的字符串是否正确 - How to check if a string in a text file is correct 如何检查文本文件以查看其是否包含字符串? - How to check a text file to see if it contains a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM