-1
BufferedReader를 사용하여 txt 파일의 정보를 읽었습니다. 나는 파일을 읽고 있지만 첫 번째 인덱스 (bookID 번호) 내 if 문을 통해 갈 선택할 수 없습니다 알아요. 문자열의 첫 번째 줄을 bookID와 일치시키고 if not 문을 건너 뛰지 않고 완료합니다. 아래 코드.자바에서 배열 인덱스를 가져 오는 데 문제가 있습니다.
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");
}
감사합니다. 고맙습니다. 당신이 사용할 필요가 문자열을 비교하려면
'inventory.txt'의 내용은 무엇입니까? – shmosel
11111, "Top Secret Twenty One - Janet Evanovich", 8.99 22222, "W Is For Wast For - Sue Grafton", 9.95 33333, "그레이 마운틴 - 존 그리샴", 14.95 44444, "부흥 - 스티븐 킹" 12.95 55555, "매일 이탈리아어 - 지아 다 드 로렌 티스 ', 18.99 66666,"거부 - 펠릭스 프란시스 " 77,777 7.99,"먼지 - 패트리샤 콘웰 " 88,888 12.99,"터미널 시티 - 린다 페어 스타 인 "10.95 99999 "악의적 인 변호사 - 존 그리샴", 15.95 11112, "The Guilty - David Baldacci", 14.95 @shmosel, bookID, book, author, price –