在java中获取数组索引时遇到问题
我使用了BufferedReader从txt文件中读取信息。我知道文件正在读取,但我无法选择第一个索引(bookID编号)来遍历if语句我希望字符串的第一行与bookID匹配,然后完成if语句,而不跳到book is not found语句。下面的代码在java中获取数组索引时遇到问题,java,arrays,bufferedreader,Java,Arrays,Bufferedreader,我使用了BufferedReader从txt文件中读取信息。我知道文件正在读取,但我无法选择第一个索引(bookID编号)来遍历if语句我希望字符串的第一行与bookID匹配,然后完成if语句,而不跳到book is not found语句。下面的代码 String bookID = jTextField7.getText(); String quantity = jTextField8.getText(); jLabel9.setText("Item #" + Inte
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");
}
我将感谢任何帮助。谢谢。要比较字符串,需要使用equals:
split[0].equals(bookId)
我做了改变,但仍然收到了相同的结果。
inventory.txt
?11111,“绝密二十一-珍妮特·埃万诺维奇”,8.99 2222,“W代表浪费的-苏·格拉夫顿”,9.95 33333,“格雷山-约翰·格里沙姆”,14.95 4444,“复兴-斯蒂芬·金”,12.95 55555,“每日意大利语-吉亚达·德·劳伦蒂斯”,18.99 66666,“拒绝-费利克斯·弗朗西斯”,7.99 77777,“灰尘-帕特里夏·康威尔”,12.99 88888,“终点城市-琳达·费尔斯坦”,10.95 99999,“无赖律师-约翰·格里沙姆”,15.95 11112,“有罪-大卫·巴尔达奇”,14.95@shmosel,书号,书,作者,价格