Results 1 to 10 of 10
Thread: [SOLVED] Search a .txt file
- 03-22-2009, 01:35 PM #1
[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
The variable tempMbrship is an intJava Code:if(tempMbrship.equals(txtMembership.getText())){ //is this a match? *****criteria u search on*****
The complete code is
Thanks in advance for any assistance/helpJava 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
-
Java Code:if(tempMbrship.equals(txtMembership.getText())){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).The variable tempMbrship is an int
- 03-22-2009, 02:37 PM #3
int vs string
Next time post the complete error you're getting.
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.Java Code:if(tempMbrship.equals(txtMembership.getText()))
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-22-2009, 03:30 PM #4
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
Thanks in advanceJava 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
-
If you have an int variable or any primitive variable (int, double, float, char,...), say:
Then you can never "dereference it". In other words, you can never call a method off of it like so:Java Code:int myIntVariable = 5;
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:myIntVariable.anyMethod(); //ERROR!
Java Code:int myIntVariable = 3; String myString = "3"; if (myIntVariable == Integer.parseInt(myString)) { System.out.println("int matches the value held by the string."); }
- 03-22-2009, 06:30 PM #6
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
-
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.So this would work when recalling the information back from the text file then ?
- 03-25-2009, 02:40 AM #8
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:
- 03-25-2009, 02:41 AM #9
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
- 03-25-2009, 11:27 AM #10
Similar Threads
-
Search in text file
By mark-mlt in forum New To JavaReplies: 6Last Post: 04-03-2009, 04:33 PM -
search file inside Zip, Rar file
By hungerToJava in forum New To JavaReplies: 0Last Post: 03-21-2009, 08:36 AM -
Search a text file
By javanewbie1979 in forum New To JavaReplies: 15Last Post: 02-09-2009, 04:46 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM -
File Search
By Juggler in forum New To JavaReplies: 2Last Post: 07-19-2008, 04:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks