[英]read specific substring with condition
我有这个来自文本文件:
134.897 134.4565 135.134
我用阅读它们:
while (( line = buffreader.readLine()) != null) {
String[] parts = line.split(" ");
String x1 = (parts[0]);
String x2 = (parts[1]);
String x3 = (parts[2]);
从文本文件中的字符串:
134.897
134.4565
135.134
我只想取这三个数字之间的不同数字:
4.897
4.4565
5.134
举更多例子:
文本文件 :
101.435
101.423
101.322
我要输入的号码:
435
423
322
我的计划是要比较每个数字,
101.435//x1
101.423//x2
101.322//x3
if x1.substring(0)=x2.substring(0)=x3.substring(0)
then remove substring(0).
我想循环此“如果”条件,直到子字符串不相同为止。
请给我一些想法,谢谢
这是可以使用的算法的框架:
List<String> inputs = ...;
int index = 0;
while (allTheStringsHaveTheSameCharacterAtIndex(inputs, index)) {
index++;
}
List<String> outputs = takeSubstringOf(inputs, index);
我让您填补空白,使用String类提供的方法和循环很容易实现这些空白。
您没有就不同长度数字的情况回答@Steve,我假设您要根据字符顺序比较数字。 另外,我假设文件始终在一行中包含三个字符串,并用一个空格分隔。
鉴于此,我希望这会有所帮助:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class FindDifference {
static File f = null;
ArrayList<String> inputs = new ArrayList<String>();
static String x1 = null;
static String x2 = null;
static String x3 = null;
private static ArrayList<String> getDifferenceList() {
ArrayList<String> ret = new ArrayList<String>();
if (x1 == null || x1.isEmpty()
|| x2 == null || x2.isEmpty()
|| x3 == null || x3.isEmpty()) {
ret.add(x1);
ret.add(x2);
ret.add(x3);
return ret;
}
int index = 0;
while (index < x1.length()
&& index < x2.length()
&& index < x3.length()) {
if (x1.charAt(index) != x2.charAt(index)
|| x1.charAt(index) != x3.charAt(index)) {
break;
}
index++;
}
ret.add(x1.substring(index, x1.length()));
ret.add(x2.substring(index, x2.length()));
ret.add(x3.substring(index, x3.length()));
return ret;
}
public static void main(String[] args) {
if (args.length > 0) {
f = new File(args[0]);
} else {
System.out.println("Please supply file path.");
return;
}
String line;
BufferedReader buffreader = null;
try {
buffreader = new BufferedReader(new FileReader(f));
while (( line = buffreader.readLine()) != null) {
String[] parts = line.split(" ");
x1 = (parts[0]);
x2 = (parts[1]);
x3 = (parts[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> output = getDifferenceList();
try {
System.out.println(output.get(0));
System.out.println(output.get(1));
System.out.println(output.get(2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我做到了SSCCE。 您只能在代码中使用getDifferenceList(将x1,x2,x3提供为字段)(在需要的地方删除“静态”)。
编辑
如我所说,您可以在任何范围(包括Android应用程序)中使用getDifferenceList。 我单独复制此方法,只需将其复制粘贴到所需的任何类中,然后通过文件路径调用它:
ArrayList<String> getDifferenceList(String filePath) {
File f = new File(filePath);
String line, x1 = null, x2= null, x3 = null;
BufferedReader buffreader = null;
try {
buffreader = new BufferedReader(new FileReader(f));
while (( line = buffreader.readLine()) != null) {
String[] parts = line.split(" ");
x1 = (parts[0]);
x2 = (parts[1]);
x3 = (parts[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> ret = new ArrayList<String>();
if (x1 == null || x1.isEmpty()
|| x2 == null || x2.isEmpty()
|| x3 == null || x3.isEmpty()) {
ret.add(x1);
ret.add(x2);
ret.add(x3);
return ret;
}
int index = 0;
while (index < x1.length()
&& index < x2.length()
&& index < x3.length()) {
if (x1.charAt(index) != x2.charAt(index)
|| x1.charAt(index) != x3.charAt(index)) {
break;
}
index++;
}
ret.add(x1.substring(index, x1.length()));
ret.add(x2.substring(index, x2.length()));
ret.add(x3.substring(index, x3.length()));
return ret;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.