Hello! I am a student (obviously, haha), and I am confused about how I need to do something in my driver class. I have a text file that I am reading in, then I decided to use a string tokenizer to parse the data. Now, I need to store the data to something, and then I have two other classes that I had to create in accordance with lab specs, and use them to get a specific output. I know the classes don't interact, but once I parse this string, how should I store the data and then call the other classes to use this data to get my output? I can't find an example of what I am trying to do. Which means I obviously don't know wth I am doing, because I am sure there are PLENTY of examples.

input file:
Chocolate Cake,260,3,12,8
Apple Pie,420,15,4,40
Lemon Meringue,80,0,2,11


Expected Output once executed correctly:

Dessert: Chocolate Cake
Cals: 260.0
Fat: 3.0g
Protein: 12.0g
Carbs: 8.0g

Dessert: Apple Pie
Cals: 420.0
Fat: 15.0g
Protein: 4.0g
Carbs: 40.0g

Dessert: Lemon Meringue
Cals: 80.0
Fat: 0.0g
Protein: 2.0g
Carbs: 11.0g

DONE

Main:

import java.io.*;
import java.util.StringTokenizer;

public class Main{
  
  public static void main(String[] args){
  
    try{
    	
      BufferedReader inputFile = new BufferedReader(new FileReader("desserts.txt"));
      
      String line = null;       
      
      while((line=inputFile.readLine())!=null){    	  
    	  
    	  
    	  
    	  StringTokenizer st = new StringTokenizer(line, ",");
    	  
    	  while(st.hasMoreTokens()) {
    		  
    		  String name = st.nextToken();
    		  double cals = Double.parseDouble( st.nextToken() );
    		  double fat = Double.parseDouble( st.nextToken() );
    		  double protein = Double.parseDouble( st.nextToken() );
    		  double carbs = Double.parseDouble( st.nextToken() );
    		  
    		  FoodFacts myFoodFacts = new FoodFacts(cals, fat, protein, carbs);        	  
        	  Dessert myDessert = new Dessert (name, myFoodFacts);
    		  
    		  
    		  System.out.println(st.nextToken()); 
    		      		     		  
    	  }
    	
    	  
      }
      
      System.out.println("DONE");
      

    }catch (IOException e) { System.err.println("Error with file");}
  }
}

Dessert Class:

public class Dessert{
	
	private String name;
	private FoodFacts info;
	
	public Dessert(String name, FoodFacts info) {
		super();
		this.name = name;
		this.info = info;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public FoodFacts getInfo() {
		return info;
	}
	public void setInfo(FoodFacts info) {
		this.info = info;
	}
	@Override
	public String toString() {
		return "Dessert [name=" + name + ", info=" + info + "]";
	}
	
	
	
	
	
	

}

FoodFacts Class

public class FoodFacts {
	
	private double cals;
	private double fat;
	private double protein;
	private double carbs;
	public FoodFacts(double cals, double fat, double protein,
			double carbs) {
		super();
		this.cals = cals;
		this.fat = fat;
		this.protein = protein;
		this.carbs = carbs;
	}
	public double getCals() {
		return cals;
	}
	public void setCals(double cals) {
		this.cals = cals;
	}
	public double getFat() {
		return fat;
	}
	public void setFat(double fat) {
		this.fat = fat;
	}
	public double getProtein() {
		return protein;
	}
	public void setProtein(double protein) {
		this.protein = protein;
	}
	public double getCarbs() {
		return carbs;
	}
	public void setCarbs(double carbs) {
		this.carbs = carbs;
	}
	@Override
	public String toString() {
		return "Cals: " + cals + "\n" + "Fat: " + fat + "g" + "\n"
				+ "Protein: " + protein + "g" + "\n" + "Carbs: " + carbs + "g";
	}
	
	

	
}

Thanks in advance!

What is your current output? Could you copy & paste so it would be easier to pin point the problem.

By the way in your Main class line 20~34, this is a dangerous habit to do that using hasNext() or hasMoreTokens() iterator. The reason is that you assume that the input format is always correct. Doing so may cause your program to crash if the format is wrong even at one place. When you call "hasMoreTokens()" means you are checking for the next token only (at least 1 token exists but not always more than 1). You should not attempt to call multiple "nextToken()" methods in one check...

Is there an empty line (which may contain white spaces only) in your input file? If so, what do you think the StringTokenizer will return when you try to parse it?

Sorry this reply is so late! Thanks for your help! I didn't get a notification for some reason, but you are right! The whitespace did mess me up, and I was able to fix it.

This question has already been answered. Start a new discussion instead.