Reading file contents into an array

Need some help reading a file with contents seperated by a "*" and placing it into an array.

Here is a snippet of the data in the file:

1000 ISLAND, SALAD DRSNG,LOCAL*1 TBSP*25
1000 ISLAND, SALAD DRSNG,REGLR*1 TBSP*60
100% NATURAL CEREAL *1 OZ*135
40% BRAN FLAKES, KELLOGG'S*1 OZ*90

Basically the Format is:     food Descrip * serving size * calories

The twist is I also need to pull the food descrip and make a combo box for it.

I know i need to use StringTokenizer, but I am lost on how to access the file and use it.
reefcrazedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
following shows how to read text from file, line by line

http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

for each line you can use the split() method to break it into an array

http://javaalmanac.com/egs/java.util.regex/ParseLine.html

or StringTokenizer

http://javaalmanac.com/egs/java.util/ParseString.html
0
reefcrazedAuthor Commented:
Read over those and did a couple more searches and currently have this code.

            comboBoxLabel = new JLabel( "Choose A Food:" );
            comboPanel.add ( comboBoxLabel );
            String s;
            Vector foodName = new Vector();
            BufferedReader in = new BufferedReader(new FileReader("FoodCalories.txt"));
            while ((s = in.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(s, "*");
              foodName.add(st.nextToken());
              foodServSize.add(st.nextToken());
                foodCalories.add(st.nextToken());
            }
            in.close();
            JComboBox comboBox = new JComboBox(foodName);

But I am getting compilation errors, I get an IO Exception file not found. I have imported the file into my project and have verified that its there. Any more suggestions?
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
just do this and see if it works

place FoodCalories.txt on c:\

and then

replace

>>          BufferedReader in = new BufferedReader(new FileReader("FoodCalories.txt"));

with

          BufferedReader in = new BufferedReader(new FileReader("c:\FoodCalories.txt"));
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

objectsCommented:
you just need to handle the exacption

          comboBoxLabel = new JLabel( "Choose A Food:" );
          comboPanel.add ( comboBoxLabel );
          String s;
          Vector foodName = new Vector();
          try {
          BufferedReader in = new BufferedReader(new FileReader("FoodCalories.txt"));
          while ((s = in.readLine()) != null)
          {
              StringTokenizer st = new StringTokenizer(s, "*");
             foodName.add(st.nextToken());
             foodServSize.add(st.nextToken());
                foodCalories.add(st.nextToken());
          }
          in.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
          JComboBox comboBox = new JComboBox(foodName);
0
reefcrazedAuthor Commented:
No didn't work like that either shivaspk.

Thanks Objects, added the exception handling and it started compiling. Still having an issue with it though, going to have to do some more reading.
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
yet you are getting the same exception??? even after using exception handling what Objects had provided???

if so now try what i had said previously along with objects code.

place that file on c:\

          comboBoxLabel = new JLabel( "Choose A Food:" );
          comboPanel.add ( comboBoxLabel );
          String s;
          Vector foodName = new Vector();
          try {
          BufferedReader in = new BufferedReader(new FileReader("c:\FoodCalories.txt"));
          while ((s = in.readLine()) != null)
          {
              StringTokenizer st = new StringTokenizer(s, "*");
             foodName.add(st.nextToken());
             foodServSize.add(st.nextToken());
                foodCalories.add(st.nextToken());
          }
          in.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
          JComboBox comboBox = new JComboBox(foodName);
0
reefcrazedAuthor Commented:
No the file not found exception is cleared up sorry. I needed to be more specific in my response.
I am still having an issue with what to do after I read the file.

I added  System.out.println(s); just afer my while statement and get the entire contents of the file outputed.
Now I need to figure out how to create an object store it some where and be able to pull just the foodName to create a comboBox.

I wrote a food class file to create objects with.

// Food Class
public class Food
{
      String food, servSize;
      int calories;
      
      public Food ( String foodItem, String foodServSize, int foodCalories)
      {
            food = foodItem;
            servSize = foodServSize;
            calories = foodCalories;
      }
      
      public void setFood ( String foodItem )
      {
            food = foodItem;
      }
      
      public String getFood()
      {
            return food;
      }
      
      public void setServSize ( String foodServSize )
      {
            servSize = foodServSize;
      }
      
      public String getServSize()
      {
            return servSize;
      }
      
      public void setCalories ( int foodCalories )
      {
            calories = foodCalories;
      }
      
      public int getCalories()
      {
            return calories;
      }
      
      public String toString()
      {
            return getFood() + "\t" + getServSize() + "\t" + getCalories();
      }
}// End Food Class

Not even sure I should use a vector for this as I do not know to much about them, but I will not learn till I do. Anymore help is appreciated.
0
objectsCommented:
try something like,( where foods is a List of Food objects):

          while ((s = in.readLine()) != null)
          {
              StringTokenizer st = new StringTokenizer(s, "*");
              Food food = new Food(st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()));
              foods.add(food);
          }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
reefcrazedAuthor Commented:
I think you guys have me on the right road, will need to take this back up in the morning. Thanks Guys.
0
Mayank SAssociate Director - Product EngineeringCommented:
Or you can also use split () to do it:

String[] array = s.split ( "*" ) ; // assuming you have Java 1.4 or higher
Food food = new Food ( s[0], s[1], Integer.parseInt ( s[2] ) ) ;
foods.add ( food ) ;
0
reefcrazedAuthor Commented:
Played with that some tonight and tweaked it and its working. Objects, thanks for the assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.