Results 1 to 6 of 6
Thread: Reading from file?
- 12-12-2010, 01:16 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
Reading from file?
hey guys, Im having trouble reading different data from one line
Here is part of my txt file:
Java Code:AEM, Intake, 242.95 AGP, Intercooler, 214.95 Apex, Turbo, 5495.00 Apexi, Intake, 154.95 Apexi, Exhaust, 769.95 Apex, Suspension, 1147.95
I know how to read the first data, ie AEM, and using the whitespace, I can read Intake also, but it also reads the "price" included, so I dont know how to break it into 3 parts.
I tried to add commas, and specific the indexes, but it doesnt work when I tried to read the price.
I also tried using this:
Java Code:readStream = new Scanner(new FileInputStream("mods.txt")).useDelimiter("," , ",");
Can someone help?
THANKS!
- 12-12-2010, 01:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 12-12-2010, 09:58 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
can u give me an example of what you mean by that?
thanks
- 12-12-2010, 10:13 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
Java Code:FileReader inputFile= new FileReader ("xxx.txt"); BufferedReader inputBuffer = new BufferedReader(inputFile); String Line = inputBuffer.readLine(); String[] record1 = Line.split(", "); /* You will have record1[0] = "AEM", record1[1] = "Intake", record1[2] = "242.95" */
- 12-12-2010, 10:58 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
Thanks mi14chal!
I wanted to ask u though, instead of storing them in an array, I would like to store each of them individually in a different variable based on if the user wants to search by brand name, model, or price
Here is my code:
Java Code:public void searchBrand() { Scanner readStreamBrand = null; String readBrand = null; String storeBrand = null; try { readStreamBrand = new Scanner(new FileInputStream("mods.txt")); System.out.print("Enter the manufacturer: "); brandName = keyboard.nextLine(); while (readStreamBrand.hasNextLine()) { readBrand = readStreamBrand.nextLine(); storeBrand = readBrand.substring(0, readBrand.indexOf(",")); if (storeBrand.equalsIgnoreCase(brandName)) { break; } else { storeBrand = null; } } if (storeBrand == null) //brand name could not be found in database { System.out.println("TunerShop does not hold the manufacturer you are looking for."); System.out.println("Contact them directly for more information!"); } else { searchPart(); } } catch (FileNotFoundException e) { System.out.println("********************************"); System.out.println("* The file could not be found! *"); System.out.println("********************************"); System.exit(0); } readStreamBrand.close(); } public void searchPart() { } public void searchPrice() { }
In the code:
Java Code:storeBrand = readBrand.substring(0, readBrand.indexOf(","));
But how would I go about doing that in my searchPart() method to store the model since the model is in the middle of 2 commas?
Same question with the price?
Can you show me by example? I learn best by it. Thanks!
- 12-13-2010, 12:07 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Split the lines you read on comma-space as has been suggested. Then you can obtain the appropriate target string (brand, model or price) from the array that split() returns.
-------------------------------
You may find it useful to parse the price string as a double: ie use parseDouble() on the price string. That way you can offer useful searches such as finding products whose price lies between two values.
Similar Threads
-
Reading file .txt
By will_java in forum Advanced JavaReplies: 5Last Post: 11-20-2010, 05:16 AM -
reading a file and writing to a file....help!!!!
By java_prgr in forum New To JavaReplies: 3Last Post: 07-26-2010, 07:53 PM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 11:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-10-2009, 12:31 AM -
reading csv file help
By fritz1474 in forum New To JavaReplies: 5Last Post: 09-04-2008, 09:41 PM
Bookmarks