我有一个带有GUI的程序,我插入了几个hexes(String),按下搜索按钮,然后找到它的代码。我从CSV文件导入我有代码的所有hexes。我想要的是,如果我进入例如 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
Edit1:这是现在适用于我的代码,想要分享它。我现在唯一的问题是我只能从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]);
}
}
}
答案 0 :(得分: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);
}
我希望你觉得它很有用。
答案 1 :(得分:1)
您可以使用谓词链来比较十六进制字符串和搜索字符串中的偶数个字符:
// 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 :(得分:0)
假设代码被声明为字符串代码[2],你必须得到你想要的“csvSplitBy”等于“,”
或明确地说:
code = line.split(",");