Link to home
Start Free TrialLog in
Avatar of reefcrazed
reefcrazed

asked on

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
Avatar of reefcrazed
reefcrazed

ASKER

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?
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"));
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);
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.
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);
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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you guys have me on the right road, will need to take this back up in the morning. Thanks Guys.
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 ) ;
Played with that some tonight and tweaked it and its working. Objects, thanks for the assistance.