-1

I have used a BufferedReader to read in information from a txt file. I know the file is reading but I cannot select the first index (bookID number) to go through my if statement. I want the first line of the string to match with the bookID and then complete the if statements without skipping to the book is not found statement. Code below.

 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"); 

}                                       

I would appreciate any help. Thank you.

  • What is the content of `inventory.txt`? – shmosel Sep 04 '16 at 23:33
  • 11111, "Top Secret Twenty One - Janet Evanovich", 8.99 22222, "W Is For Wasted - Sue Grafton", 9.95 33333, "Gray Mountain - John Grisham", 14.95 44444, "Revival - Stephen King", 12.95 55555, "Everyday Italian - Giada de Laurentiis", 18.99 66666, "Refusal - Felix Francis", 7.99 77777, "Dust - Patricia Cornwell", 12.99 88888, "Terminal City - Linda Fairstein", 10.95 99999, "Rogue Lawyer - John Grisham", 15.95 11112, "The Guilty - David Baldacci", 14.95 @shmosel, it's bookID, book, author, price – Nakiya Russell Sep 06 '16 at 16:58

1 Answers1

2

To compare strings you need to use equals:

split[0].equals(bookId)
Mariano L
  • 1,809
  • 4
  • 29
  • 51