473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reading an inventory.txt file and placing the price and item into an array

14
The text file is below:

21.95 Water Filter Pack
.51 Pork and Beans
1.79 Chicken Salad Sandwich
5.99 Coke 24-pack
3.79 Barbara's Oat Bran Cereal
2.19 Sandwich Bags
1.07 Potato Chips

Here is part of my code to read the text file.

Expand|Select|Wrap|Line Numbers
  1.     try{
  2.            FileReader fr=new FileReader(filename);
  3.            BufferedReader br=new BufferedReader(fr);
  4.  
  5.            String line;
  6.            int index=0;
  7.  
  8.                    while((line=br.readLine())!=null){
  9.                         StringTokenizer tokenizer=new StringTokenizer(line,"  ");
  10.  
  11.                           while(tokenizer.hasMoreTokens()){
  12.  
  13.                               String item=tokenizer.nextToken();
  14.                               Double price=Double.parseDouble(item);
  15.  
  16.  
  17.  
  18.                            System.out.println(price);
  19.  
  20.                } 
  21.  
  22.  
  23.            }
  24.  
  25.          br.close();
  26.             }    
I used a printline statement in order to determine whether or not all of the prices will be read properly. However, after running this code, it only prints the first number which is 21.95 and it doesn't loop through the rest.
Sep 14 '06 #1
2 2853
The text file is below:

21.95 Water Filter Pack
.51 Pork and Beans
1.79 Chicken Salad Sandwich
5.99 Coke 24-pack
3.79 Barbara's Oat Bran Cereal
2.19 Sandwich Bags
1.07 Potato Chips

Here is part of my code to read the text file.
First of all your code is really compuationally expensive. The BufferedReader is fine, but StringTokenizer is a pig. Try something of the following using the RegExp packages from jakarta

Expand|Select|Wrap|Line Numbers
  1. RECompiler rec = new RECompiler();
  2. REProgram rep = rec.compile("^([\\d\\.]+)\\s+(.*)");
  3. RE myre = new RE(rep);
  4.  
  5. BufferedReader br = new BufferedReader(new FileReader(new File(<path>)));
  6. String wkline = null;
  7. while((wkline = br.readLine()) != null) {
  8.   if(myre.match(wkline) {
  9.     try {
  10.       double wkprice = Double.parseDouble(myre.getParen(1));
  11.       String item = myre.getParen(2);
  12.       <work to insert in array here>
  13.     } catch (NumberFormatException nfe) {
  14.       nfe.printStackTrace(System.err);
  15.     }
  16.   }
  17. }
  18. br.close();
  19.  
This should address both the performance and the issue you were dealing with. You have to remember that the StringTokenizer you're using is looking for spaces and will tokenize the entire line wherever there is a space.
Sep 19 '06 #2
r035198x
13,262 8TB
your inner while loop should be throwing a number format exception when it tries to access the second token. It should be something like this
Expand|Select|Wrap|Line Numbers
  1. boolean done = false;
  2. while(tokenizer.hasMoreTokens() && done == false) {
  3.      int price = Integer.parseInt(tokenizer.nextToken());//get only the first word
  4.      done == true;
  5. }
java also has
java.util.regex package which is probably what you would want to use here for the reason already given.
Sep 20 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
4
nexcompac
by: nexcompac | last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder. Still getting used to the whole java world and I am...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
11
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your...
3
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory...
5
by: cblank | last post by:
I'm having some trouble with my inventory program. Its due tom and my teacher is not wanting to help. He keeps giving me a soluction that is not related to my code. I have everything working except...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class...
16
by: lilsugaman | last post by:
I have to assignment which includes the following: Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.