我使用了BufferedReader来读取txt文件中的信息。我知道文件正在读取但我无法选择第一个索引(bookID号)来通过我的if语句。 我希望字符串的第一行与bookID匹配,然后完成if语句而不跳到书中找不到语句。下面的代码。
String bookID = jTextField7.getText();
String quantity = jTextField8.getText();
jLabel9.setText("Item #" + Integer.toString(currItem) + "info");
jButton8.setEnabled(false);
jButton9.setEnabled(true);
BufferedReader br = null;
String sCurrentLine;
try {
//String sCurrentLine;
br = new BufferedReader(new FileReader("inventory.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String[] split = sCurrentLine.split(",");
System.out.println(sCurrentLine);
if (split[0] == bookID) {
int discount = calculateDiscount(Integer.parseInt(quantity));
double itemSubTotal = (Integer.parseInt(quantity)*new BigDecimal(split[2]).doubleValue()*(discount/100));
subTotal += itemSubTotal;
String info = split[0] + " " + split[1] + " " + split[2] + " " + quantity + " " + Integer.toString(discount) + " " + (Integer.parseInt(quantity)*new BigDecimal(split[2]).doubleValue()*(discount/100));
books.add(info);
jTextField9.setText(info);
return;
}
}
}
catch (IOException e ){
e.printStackTrace();
}
finally {
try {
if (br != null)br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "Book ID " + bookID + " not in File");
}
我将不胜感激任何帮助。谢谢。
答案 0 :(得分:2)
要比较您需要使用等于的字符串:
split[0].equals(bookId)