Results 1 to 2 of 2
  1. #1
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default search in file, help

    Hey, I go this piece of code in my product.java class:
    public void loadProduct(JTextField srcfield1, JTextField srcfield2, JTextField srcfield3, JTextField srcfield4,
    JTextField srcfield5, String strProductname)
    {
    String record = null;
    FileReader in = null;
    try {
    in = new FileReader("Products.txt");
    BufferedReader br= new BufferedReader(in);
    record = new String();
    while (br.readLine() != null) {

    if (record.contentEquals(strProductname))
    {

    srcfield1.setText(record);
    srcfield2.setText(br.readLine());
    srcfield3.setText(br.readLine());
    srcfield4.setText(br.readLine());
    srcfield5.setText(br.readLine());
    }
    else
    record = br.readLine();
    }
    br.close();
    } catch (IOException ioe) {
    JOptionPane.showMessageDialog(null, "Could not find product details"
    );
    }
    }

    When I run the program, it does find the text file, but it only reads the last 4 lines of the text file. It is meant to find the productname and then display the next 5 lines. Could anyone help me how to correct this code to make it work?

    I use this function in my MainProgram as follows:

    String productName =(String)ProductComboBox.getSelectedItem();
    myProduct.loadProduct(ProductNameTextField, ProductIdTextField, WeightTextField, PriceNoVatTextField, ExpDateTextField,
    productName);

    I am not sure where I went wrong:)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: search in file, help

    What happens to the line read by this:
    while (br.readLine() != null) {
    It looks like you are not using what is read.

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2011, 06:15 AM
  2. [SOLVED] Search a .txt file
    By dbashby in forum New To Java
    Replies: 9
    Last Post: 03-25-2009, 11:27 AM
  3. search file inside Zip, Rar file
    By hungerToJava in forum New To Java
    Replies: 0
    Last Post: 03-21-2009, 08:36 AM
  4. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  5. File Search
    By Juggler in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •