Home Segments Index Top Previous Next

456: Mainline

Once you know how to read one calorie description, you can, of course, write a method that reads a whole file full of calorie descriptions and stores them in an OrderedCollection instance. You know that every description has been read when the nextWord message, sent to the stream, answers nil.

Food method definition • class 
collectFrom: aFile 
  |inputStream collection food| 
  inputStream := File pathName: aFile. 
  collection := OrderedCollection new. 
  [(food := inputStream nextWord) notNil] 
    whileTrue: 
      [collection add:  
         (Food new name: food; 
           fCalories: inputStream nextWord asInteger; 
           cCalories: inputStream nextWord asInteger; 
           pCalories: inputStream nextWord asInteger)]. 
  inputStream close. 
  ^ collection