[英]Parsing strings with split JAVA
I'm trying to find an object in a list from a text file 我正在尝试从文本文件中找到列表中的对象
Example: 例:
By inserting 98123742 by input with scanner, i want to find that string. 通过使用扫描仪输入插入98123742,我想找到该字符串。 I tried to do this: 我试图这样做:
private static void inputCode() throws IOException {
String code;
String line = null;
boolean retVal = false;
System.out.println("\ninsert code: ");
code = in.next();
try {
FileReader fileReader = new FileReader("SHOP.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
String[] token = line.split(";");
if (token[0].equals(code) && token[1].equals(code)) {
retVal = true;
System.out.println(line);
}
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("impossible open the file " + fileName);
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
System.out.println(retVal);
}
How can i print "-H;U;30;€12,00;98123742;Hammer" inserting "98123742" (that is the code of the product) ? 如何打印插入“ 98123742”(即产品代码)的“ -H; U; 30;€12,00; 98123742; Hammer”?
Why are you splitting in the first place? 你为什么首先分裂? For such a simple usecase, and with that line format, I'd go with 对于这样一个简单的用例,使用这种行格式,
line.contains(";" + code);
Not much else to do. 没什么可做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.