Results 1 to 10 of 10
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] Search a .txt file

    I have a project that stores information to a txt file, I have tried to convert this so that I am able to use it in another project however the variables differ.

    I cannot seem to get my head round why and how I construct the 1 line of code that is causing the error.

    I would be grateful for someones help

    This is only posted in this forum!

    The line of code that is causing the problem is

    Java Code:
                   if(tempMbrship.equals(txtMembership.getText())){ //is this a match?  *****criteria u search on*****
    The variable tempMbrship is an int

    The complete code is

    Java Code:
    		int tempMbrship=0; // integer
            double tempBmindex;
            tempBmindex = Double.parseDouble("0");
            double tempRdate;
            tempRdate = Double.parseDouble("0");
            boolean endofFile = false;
            boolean openOk = true;
            boolean search = false;
    
            mshipRecord record = null;
    
       try{
           input= new ObjectInputStream(new FileInputStream(file)); //open file to read
       }
       catch (IOException e){ //trap open error
           optPane1.showMessageDialog(this,"Error in File","Error",optPane1.ERROR_MESSAGE);
           openOk = false;}
    
    
       if (openOk == true) { //if opened OK
           while ((endofFile == false) && (search== false)) { //if not eof or not found match
               try{
                   record = (mshipRecord)input.readObject();//read record
                   tempMbrship = record.getMbrship();
                   tempBmindex = record.getBmindex();
                   tempRdate = record.getRdate(); //move record fields into temp storage
                   
    
    	}
               catch (EOFException e){
                   endofFile = true; //trap EOF
                   optPane1.showMessageDialog(this, "MEMBER NOT FOUND","Error",optPane1.ERROR_MESSAGE);
                   txtMembership.setText("");
                   txtDate.setText(""); //display blank fields
                   txtBmi.setText("");
    
    	}
               catch (ClassNotFoundException e){ //trap record class
               optPane1.showMessageDialog(this, "Error in File","Error", optPane1.ERROR_MESSAGE);
                       }
               catch (IOException e){ //trap record class error
                   optPane1.showMessageDialog(this,"Error in File","Error", optPane1.ERROR_MESSAGE);
               }
    
                   if(tempMbrship.equals(txtMembership.getText())){ //is this a match?  *****criteria u search on*****
                 //  txtMembership.setText(Integer.toString(tempMbrship));
                 //  txtDate.setText(double.toString(tempRdate)); //display stored fields
                 //  txtBmi.setText(double.toString(tempBmindex));
                   txtMembership.setText(Integer.toString(record.getMbrship()));
                   txtDate.setText(Double.toString(record.getRdate()));
                   txtBmi.setText(Double.toString(record.getBmindex()));
    			   search = true;
               }//end of IF match
               } //end of eof, search loop
    		   }//end of openOK loop
    Thanks in advance for any assistance/help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    if(tempMbrship.equals(txtMembership.getText())){
    The variable tempMbrship is an int
    You appear to be trying to call the method "equals" on an int which is a primitive. Since primitives aren't objects, they don't have any methods at all much less equals, and so this attempt will fail. For ints you must use == to check for equality, and variable on the right must be an int too (you may need to parse it into an int).

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default int vs string

    Next time post the complete error you're getting.
    Java Code:
    if(tempMbrship.equals(txtMembership.getText()))
    You can't compare an int (tempMbrship) to a string (txtMembership.getText()). I would suggest that your program either handle all membership numbers as ints (which sounds correct) or as Strings. Then you can compare them without problems.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    Fubarable, so just to make sure I am getting what you are saying.

    If I declared txtMembership as an int at the start and remove the .equals and replace with == this should work?

    CJSLMAN - I have now declared as int's however the construction of the code is causing me the problem.

    The error I have now is 'int cannot be dereferenced' on 3 lines of code

    As I say its the construction of the following is the things I am having problems with

    Java Code:
    if(tempMbrship==(txtMembership.getText())){ //is this a match?  *****criteria u search on*****
                   txtMembership.setText(Integer.toString(record.getMbrship()));
                   txtDate.setText(Double.toString(record.getRdate()));
                   txtBmi.setText(Double.toString(record.getBmindex()));
    			   search = true;
               }//end of IF match
               } //end of eof, search loop
    		   }//end of openOK loop
    Thanks in advance

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you have an int variable or any primitive variable (int, double, float, char,...), say:
    Java Code:
    int myIntVariable = 5;
    Then you can never "dereference it". In other words, you can never call a method off of it like so:
    Java Code:
    myIntVariable.anyMethod(); //ERROR!
    If you want to compare an int with a String that holds the String representation of an int, you have to first parse the String, then compare the two:

    Java Code:
    int myIntVariable = 3;
    String myString = "3";
    
    if (myIntVariable == Integer.parseInt(myString))
    {
      System.out.println("int matches the value held by the string.");
    }

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    So this would work when recalling the information back from the text file then ?

    Sorry for my ignorance with java but I am trying to learn

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So this would work when recalling the information back from the text file then ?
    I've no idea. The only way to find out is to try it and see. As we've discussed before though, I think that you are mistaken by trying to test these concepts within a large program. First work on solving this issue in a small program whose purpose is to only work on this problem. Only when this works, should you add the code to your larger program. Otherwise there are too many confounding issues.

  8. #8
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    I have done as suggested, I have an app with only 3 text fields, my aim is to let me search 1 by 1 the text file, there are no errors in the code but for some reason when I click on the search button the try catch comes up straight away saying 'MEMBER NOT FOUND'

    I now have the field I want to search by as a String as suggested earlier in this thread.

    Can anyone see what the problem is and if so point me in the right direction???

    Thanks in advance for your help.

    :confused:

  9. #9
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Here is the code to go with the above message.

    Java Code:
    // TODO add your handling code here:
            String tempMbrship="";
            Double tempJdate;
            tempJdate = Double.parseDouble("0");
            Double tempBmiR;
            tempBmiR = Double.parseDouble("0");
    
            boolean endofFile = false;
    		boolean openOk = true;
    		boolean search = false;
    
       MshipRecord record;
    
       try{
           input= new ObjectInputStream(new FileInputStream(file)); //open file to read
       }
       catch (IOException e){ //trap open error
           optPane1.showMessageDialog(this,"Error in File","Error",optPane1.ERROR_MESSAGE);
           openOk = false;}
    
       if (openOk == true) { //if opened OK
           while ((endofFile == false) && (search== false)) { //if not eof or not found match
               try{
                   record = (MshipRecord)input.readObject();//read record
                   tempMbrship = record.getMbrship();//move record fields into temp storage
                   tempJdate = record.getRdate();
                   tempBmiR = record.getBmindex();
               }
            catch (EOFException e){
                   endofFile = true; //trap EOF
                   optPane1.showMessageDialog(this, "MEMBER NOT FOUND","Error",optPane1.ERROR_MESSAGE);
                   txtMembership.setText("");
                   txtJdate.setText(""); //display blank fields
                   txtBmi.setText("");
            }
               catch (ClassNotFoundException e){ //trap record class
               optPane1.showMessageDialog(this, "Error in File","Error", optPane1.ERROR_MESSAGE);
                       }
               catch (IOException e){ //trap record class error
                   optPane1.showMessageDialog(this,"Error in File","Error", optPane1.ERROR_MESSAGE);
                   }
               if(tempMbrship.equals(txtMembership.getText())){ //is this a match?  *****criteria u search on*****
                   txtMembership.setText(tempMbrship);//display stored fields
                   txtJdate.setText(Double.toString(tempJdate));
    			   txtBmi.setText(Double.toString(tempBmiR));
                   search = true;
               }//end of IF match
               } //end of eof, search loop
               }//end of openOK loop

  10. #10
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Solved

    This morning when I tried this code everything worked as it should so I cant say what the problem was but everything ok up to now.

Similar Threads

  1. Search in text file
    By mark-mlt in forum New To Java
    Replies: 6
    Last Post: 04-03-2009, 05:33 PM
  2. search file inside Zip, Rar file
    By hungerToJava in forum New To Java
    Replies: 0
    Last Post: 03-21-2009, 09:36 AM
  3. Search a text file
    By javanewbie1979 in forum New To Java
    Replies: 15
    Last Post: 02-09-2009, 05:46 PM
  4. Replies: 7
    Last Post: 11-19-2008, 03:09 PM
  5. File Search
    By Juggler in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 05: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
  •