Question: Java Question: Instructions: The goal of this project is to create a Food Diary. This diary should contain breakfast, lunch, dinner and Snacks that you
Java Question:
Instructions:
The goal of this project is to create a Food Diary. This diary should contain breakfast, lunch, dinner and Snacks that you consume during the day. The user should be able to enter their food items and save it as a csv file. The user should be able to keep appending to the file.
This is what I have so far but I'm not sure where to close my scanner:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class App {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new File("test.csv"));
StringBuilder sb = new StringBuilder();
sb.append("Date");
sb.append(',');
sb.append("FoodTime");
sb.append(',');
sb.append("FoodItem");
sb.append(',');
sb.append("Calories");
sb.append(' ');
int exit;
do {
Scanner sc = new Scanner(System.in);
System.out.println("Date : ");
String Date = sc.next();
System.out.println("Meal Time: ");
String FoodTime = sc.next();
System.out.println("Food Item : ");
String FoodItem = sc.next();
System.out.println("Calories : ");
String Calories = sc.next();
sb.append(Date);
sb.append(',');
sb.append(FoodTime);
sb.append(',');
sb.append(FoodItem);
sb.append(',');
sb.append(Calories);
sb.append(' ');
pw.write(sb.toString());
System.out.println("Please enter 0 to exit, 1 to continue : ");
exit = sc.nextInt();
} while (exit != 0);
System.out.println("done!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
