• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help searching for string in file  RSS feed

 
Mosty Mohamed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am required to make a program to read product name ,its quantity and its price and write it in a file
then make another button to read what i've wrote in that file
the problem comes with a third button that requires me to search for a certain product in that file and display its quantity and its price
as far as i have tried i can't get it right i'll put my code on here

and as far as i know we aren't allowed to use the buffer method we must do it using the basics
i don't need a code i just would like pointers on how to do it
thanks in advance
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Forget all about buttons for the time being. You can add buttons later. Write down very simply how you would read from that file, or write to that file, record the different values and later find the values from a keyword/definition/product name. When you have done that, you will be able to turn it into code easily. Make sure your application has a public interface which your GUI can access.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your requirements state "...product name ,its quantity and its price...". This immediately suggests creating a class "Product" with those fields. From there I would suggest implementing a method "readRecord(BufferedReader reader)" that can read one line from a text file. For testing purposes, implement a "toString()" method. Slap that together with a minimal "main()" method, create a file to read by hand with 2 or more lines, and get it all to work.

Edit: Alternatively, it could be readRecord( String str ).
 
Mosty Mohamed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i appreciate both of answers
i tried making a new class for the Product and did what you said but i still couldn't do the Search part not sure if i am doing it right or not
the search code ihave is this

thanks for advance
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling
This method returns a String but you aren't doing anything with it. Assuming "SP" is your Product class, you'll need a constructor that takes this String, parses it, and sets the values of the corresponding fields.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In you line
you should not use "==" to compare two Strings. "==" is used to compare references but NOT the values that they refer to. For that, use the equals() method.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There's a minor naming issue here. Variable names must start with a lower case letter.
If "t" is an instance of your Product class then the field name should have been "name", as in "t.name", per your requirements.
Ideally these fields would have been declared as "private" and you would have provided getter methods for them, so, t.name would then become t.getName().
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thirdly, you'll need to have a List of Product objects to search. You could also do this with an array of Product's but then you'd have to keep track of how many Product's you've put into your array. As you construct your Product objects add them to the List (or array).

Your search should then loop through the List (or array) to find the Product with the matching "name".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!