![](/img/trans.png)
[英]Java - Parsing strings - String.split() versus Pattern & Matcher
[英]Parsing strings with split JAVA
我正在嘗試從文本文件中找到列表中的對象
例:
通過使用掃描儀輸入插入98123742,我想找到該字符串。 我試圖這樣做:
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);
}
如何打印插入“ 98123742”(即產品代碼)的“ -H; U; 30;€12,00; 98123742; Hammer”?
你為什么首先分裂? 對於這樣一個簡單的用例,使用這種行格式,
line.contains(";" + code);
沒什么可做的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.