假设我有一个文本文件,每行有两个字符串:
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
如何在阅读时检查文本文件是否包含一个或两个字符串?
答案 0 :(得分:2)
使用split(" ")
,获取返回的数组长度,然后解析数组中的最后两个String数组项,项length - 1
和length - 2
,作为双精度数,然后遍历在最后两个项目之前的剩余字符串项目,并将它们组合为城市字符串。像,
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();
}
答案 1 :(得分:1)
使用正则表达式。
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
答案 2 :(得分:1)
最适合您的工具是regexp。
由于您可以确定您的两个号码不包含任何空格,因此您可以将其定义为"\S+"
,并将其他任何内容与name
的模式匹配。
这允许你在name
部分中拥有任意数量的单词(以及字面上的任何其他内容),同时允许以任何格式(如科学记数法)包含数字,只要它们没有“{1}}部分。里面有空格。
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