0

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. Here is what I have so far When I write my variables f and t to excel they are in the same cell how can I make it so they are in separate columns

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

    public class App {


    public static void main(String[] args) {
    BufferedWriter bw = null;
    FileWriter fw = null;
    FileOutputStream FileName = new FileOutputStream( "MyFooDiary.csv");
    System.out.println("Welcome to your food diary");
    System.out.println("What is the name of your Food?");
    Scanner x = new Scanner(System.in);
    String f = x.nextLine();
    System.out.println("Was this breakfast, lunch, dinner, or a snack");
    Scanner y = new Scanner(System.in);
    String t = y.nextLine();






    try {

        File file = new File(FileName);

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        // true = append file
        fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);

        bw.write("Date, Food Time, Food Name, Calories, Carbohydrates, 
        Sugars, Fiber, Protein, Total Fat ");
        bw.write(t);
        bw.write(f);;
        System.out.println("Done");

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (bw != null)
                bw.close();

            if (fw != null)
                fw.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }
    }

} }

1 Answers1

0

You need to add a comma , between values to have them in different cells.

Try this method to check if the end of t and the start of f do not have a ",". If neither has a comma then add one to the end of t:

    bw.write(t);
    //check if the end of t and the start of f do not have a ",". If neither has a comma then add one to the end of t
    if (t.endsWith(",") == false && f.startsWith(",") == false)
    {
        t = t + ",";
    }
    bw.write(f);

I would suggest writing a special method to do a check like this on two variables so that you can call it from anywhere, also I note that you are missing a "," at the end of this line Sugars, Fiber, Protein, Total Fat ");. is should have Fat, "); (notice the comma).

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • How would I go about it moving down a row each time the program runs? – Andrew Mueller May 04 '17 at 00:27
  • Do you mean you want to run your code multiple times on the same file and just keep adding to it? You are doing that already with `fw = new FileWriter(file.getAbsoluteFile(), true);`. However if you just want to put text in rows instead of columns then just add a new line character whenever you want a new row like this: `bw.write("\n");` or `bw.write(System.getProperty("line.separator"))` – sorifiend May 04 '17 at 01:32
  • Okay and last question how can I make it so that this bw.write("Date, Food Time, Food Name, CALORIES, Carbohydrates only writes the first time program is ran – Andrew Mueller May 04 '17 at 01:39
  • You should ask a new question for different things. And don't forget to mark answers as accepted. To do what you asked you need to read the file and check the first row of csv to make sure it is OK, then you can use the rest of your code. – sorifiend May 04 '17 at 01:50
  • I am new to this thanks for the advice! Could you possibly lead me to a link or somewhere I can get information on how to do this? – Andrew Mueller May 04 '17 at 01:59
  • Here is a good answer: http://stackoverflow.com/a/24666849/1270000 `BufferedReader brTest = new BufferedReader(new FileReader(myTextFile));` then `text = brTest .readLine();` – sorifiend May 04 '17 at 02:01