-1

Can anyone help me out with my code below? I have been working on this for hours and can't seem to get my output to write out to a file no matter what I do. I'm a high school student and asked my teacher for help, but what she sent me didn't help and I'm now at a complete loss. I'm taking this course independently on my own so hoping someone from the stackoverflow family could provide me some pointers? It's not from lack of trying on my part. I'm just at a loss. Thank you in advance!!

/**
 * 
 */
package distanceTraveledReport;
import java.util.Scanner;
import java.io.*;

/**
 * @author Jerin
 *
 */
public class DistanceTraveledReport {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //prints out name and project I am working on
        System.out.println("Jerin Anderson, This is my Distance Traveled Report");
                
        //create scanner
        Scanner scanner = new Scanner(System.in);
        double vehicleSpeed = -1; //to hold the speed from the vehicle
        int hoursTraveled = 0; //to hold the hours traveled by the vehicle
        double distanceTraveled; //to hold the distance traveled for each hour
                
        //while loop, inputs validation
        while ( vehicleSpeed < 0 ) 
                {
                    System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
                    vehicleSpeed = scanner.nextDouble();
                }
        while (hoursTraveled < 1) 
                {
                    System.out.println("Enter the number of hours traveled by the vehicle" );
                    hoursTraveled = scanner.nextInt();
                }
                
        //heading at the top and lines to separate table
        System.out.println(" Hour\t Distance Traveled");
        System.out.println("---------------------------");
        
                {
        //write the table of distances
        distanceTraveled = 1;
        while (distanceTraveled <= hoursTraveled)
        {
            //Display the distance for this period.
            System.out.println(distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed);
            
            //Increment period
            distanceTraveled++;
        }
        
        //Close the file.
        System.out.close();
        System.out.println("Report written to DistanceReport.txt");
    }

}
    
}
Anna Smith
  • 17
  • 3
  • 2
    I would recommend using [this](https://www.w3schools.com/java/java_files_create.asp) as a reference for writing to files. – Adrian Russo Jan 14 '21 at 02:20
  • 3
    https://docs.oracle.com/javase/tutorial/essential/io/file.html – PM 77-1 Jan 14 '21 at 02:22
  • 1
    Poor title. Rewrite to summarize your *specific* technical issue. – Basil Bourque Jan 14 '21 at 02:42
  • You have not actually asked a question. This site is not for general discussion and guidance. This site is for questions narrowly-focused on a specific technical issue that is likely to have a specific solution. What exactly is your problem with writing out data to a file? How have none of the [hundreds (thousands) of other Questions and Answers](https://duckduckgo.com/?q=site%3Astackoverflow.com+java+write+data+to+file&t=osx&ia=web) on the topic not addressed your specific issue? – Basil Bourque Jan 14 '21 at 02:43
  • 1
    Duplicate of: [*How do I create a file and write to it in Java?*](https://stackoverflow.com/q/2885173/642706) – Basil Bourque Jan 14 '21 at 02:46

1 Answers1

0

You must realize that System.out.println() does not write to a file, but FileWriter does. The fixes to the code are highlighted with //TODO.

package distanceTraveledReport;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
     * @author Jerin
     *
     */
    public class DistanceTraveledReport {

        /**
         * @param args
         */
        public static void main(String[] args) throws IOException {
            // TODO "TODO check this out" marks addition of FileWriter fileWriter.
            //TODO check this out
            //TODO make sure you change the filename

First open the path of the .txt file, because the current path is not for your file (see "cole.henrich/Downloads...").

First Copy the Path of the .txt file

Then copy the absolute path:

Absolute Path

Then paste it into the parameters of FileWriter: Paste Path.

The code continued:

FileWriter fileWriter = new FileWriter("/Users/cole.henrich/Downloads/StackOverFlow/src/distanceTraveledReport/DistanceReport.txt");
            fileWriter.write("The FileWriter writes to the file. System.out.println() does not.\n");

            //prints out name and project I am working on
            System.out.println("Jerin Anderson, This is my Distance Traveled Report");

            //create scanner
            Scanner scanner = new Scanner(System.in);
            double vehicleSpeed = -1; //to hold the speed from the vehicle
            int hoursTraveled = 0; //to hold the hours traveled by the vehicle
            double distanceTraveled; //to hold the distance traveled for each hour

            //while loop, inputs validation
            while ( vehicleSpeed < 0 )
            {
                System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
                vehicleSpeed = scanner.nextDouble();
            }
            while (hoursTraveled < 1)
            {
                System.out.println("Enter the number of hours traveled by the vehicle" );
                hoursTraveled = scanner.nextInt();
            }

            //heading at the top and lines to separate table
            System.out.println(" Hour\t Distance Traveled");
            System.out.println("---------------------------");

            {
                //write the table of distances
                distanceTraveled = 1;
                while (distanceTraveled <= hoursTraveled)
                {
                    //Display the distance for this period.
                    String display = ""+ (distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed) + "\n";
                    System.out.println(display);
                    //TODO check this out
                    fileWriter.write(display);
                    //Increment period
                    distanceTraveled++;
                }

                //Close the file.
                //TODO check this out
                fileWriter.close();
                //TODO check this out: DO NOT close the System.out. Close FileWriter fileWriter.
                System.out.println("Report written to DistanceReport.txt");
            }

        }

    }

Make sure you understand FileWriter. Read the links in the comments. Here is your output, in the file (not System.out).

The FileWriter writes to the file. System.out.println() does not.
    1.0     65.0
    2.0     130.0
    3.0     195.0
    4.0     260.0
    5.0     325.0
    6.0     390.0
    7.0     455.0
Cole Henrich
  • 155
  • 3
  • 17