>我有一个带有GUI的程序,我在其中插入了几个十六进制(字符串(,按下搜索按钮,然后我找到了它的代码。我从CSV文件中导入我拥有代码的所有十六进制。我想要的是,如果我输入例如:11 D7 E2 FA
,我的程序只会搜索第二个半字节,x
意思是忽略:x1 x7 x2 xA
,如果它在 CSV 中找到类似的东西,它会给我它的代码。这就是我到目前为止所拥有的,这只在字符串匹配时找到我的情况。
codeOutputField.setText("");
String input = hexEntryField.getText();
try {
br = new BufferedReader(new FileReader(FIS));
while ((line = br.readLine()) != null) {
code = line.split(csvSplitBy);
if (input.equals(code[0])) {
codeOutputField.setText(code[1]);
}
}
}
示例 CSV:
01 5F 1E CE,0055
01 5F 13 D0,0062
01 5E 36 FE,0101
00 5E 36 FF,1002
这是现在对我有用的代码,想分享它。我现在唯一的问题是我只能从bat
文件运行jar
文件,双击不起作用。我不知道为什么。
String input = hexEntryField.getText();
String[] myStringArray = input.split("");
codeOutputField.setText("");
try {
br = new BufferedReader(new FileReader(FIS));
while ((line = br.readLine()) != null) {
code = line.split(csvSplitBy);
List<String> items = Arrays.asList(code[0].split(""));
System.out.println(items);
if (myStringArray[1].equals(items.get(1))
&& myStringArray[4].equals(items.get(4))
&& myStringArray[7].equals(items.get(7))
&& myStringArray[10].equals(items.get(10))) {
codeOutputField.setText(code[1]);
}
}
}
您的问题更多的是解析每一行。 我会组合一些正则表达式:
public class Record {
private static final Pattern HEX_VALUE = Pattern.compile("[A-F0-9][A-F0-9]");
//...
public static Record from(String line) throws Exception {
Record record = new Record();
String[] parts = line.split(",");
if (parts.length != 2) {
throw new Exception(String.format("Bad record! : %s", line));
}
record.code = parts[1];
String hexes[] = parts[0].split("\s");
if (hexes.length != 4) {
throw new Exception(String.format("Bad record! : %s", line));
}
for (String hex: hexes) {
if (!HEX_VALUE.matcher(hex).matches()) {
throw new Exception(String.format("Bad record! : %s", line));
}
}
record.hex1 = hexes[0];
record.hex2 = hexes[1];
record.hex3 = hexes[2];
record.hex4 = hexes[3];
return record;
}
...
@Override
public boolean equals(Object obj) {
boolean ret = false;
if (obj instanceof Record) {
Record r = (Record) obj;
ret = equalsSecondCharacter(this.hex1, r.hex1)
&& equalsSecondCharacter(this.hex2, r.hex2)
&& equalsSecondCharacter(this.hex3, r.hex3)
&& equalsSecondCharacter(this.hex4, r.hex4);
}
return ret;
}
...
然后只需搜索记录列表。 在示例中,我使用了Apache Commons Collections过滤:
while((line = br.readLine()) != null) {
records.add(Record.from(line));
}
// Check into the list
Collection<Record> filtered =
CollectionUtils.select(records, new EqualPredicate<Record>(inputRecord));
System.out.println("Results:");
for (Record rec: filtered) {
System.out.println(rec);
}
我希望你觉得它有用。
您可以使用谓词链来比较十六进制字符串和搜索字符串中的偶数字符:
// assume that we have an array of hexes
String[] hexes = {"015F1ECE", "015F13D0", "015E36FE", "005E36FF"};
// user input
String search = "035D3EFA";
// conditions to check
Predicate<String> predicateChain = IntStream
// even numbers 0, 2, 4, 6
.range(0, 4).map(i -> i * 2)
// compare even characters from the
// current string and the search string
// Stream<Predicate<String>>
.mapToObj(i -> (Predicate<String>)
str -> str.charAt(i) == search.charAt(i))
// reduce a stream of predicates
// to a single predicate chain
.reduce(Predicate::and)
// otherwise the condition is not met
.orElse(p -> false);
// output the filtered array
Arrays.stream(hexes).filter(predicateChain).forEach(System.out::println);
输出:
015E36FE
005E36FF
<小时 />另请参阅:在字符串数组中查找相似的字符串
假设代码被声明为字符串代码[2],你必须得到你想要的东西,使"csvSplitBy"等于",">
或明确:
code = line.split(",");