Mark Geter
Mark Geter

Reputation: 11

How to place Output from java to a .txt file.? JAVA

Currently trying to get the output of this program to a .txt file. I have to show the output in the console and also have it shown in the .txt file. I've looked and haven't seen a complete answer that would help me understand how to do it. Here is the program I'm working on. (complete beginner).

/*
 * Write a program that asks the user to enter today’s sales for five stores. The program should display a bar chart comparing each store’s sales. Create each bar in the bar chart by displaying a row of asterisks. Each asterisk should represent $100 of sales. Here is an example of the program’s output:
 *
 *  Enter today's sales for store 1: 1000 [Enter]
 *  Enter today's sales for store 2: 1200 [Enter]
 *  Enter today's sales for store 3: 1800 [Enter]
 *  Enter today's sales for store 4: 800 [Enter]
 *  Enter today's sales for store 5: 1900 [Enter]*
 *  SALES BAR CHART
 *  Store 1: **********
 *  Store 2: ************
 *  Store 3: ******************
 *  Store 4: ********
 *  Store 5: *******************
 */

import java.util.Scanner;


public class BarChart {
    public static void main(String[] args)
    {
        double[] store = new double[5];
        
        
        Scanner userInput = new Scanner(System.in);
        
        for(int i = 0; i < 5; i++)
        {
            System.out.printf("Enter today\'s sales for store %d: ", i + 1);
            store[i] = userInput.nextDouble();
        }
        
        System.out.println("\nSALES BAR CHART");
        
        for(int i = 0; i < 5; i++)
        {
            System.out.printf("Store %d: ", i + 1);
            
            for(int c = 0; c < store[i]/100; c++)
                System.out.print("*");
            
            System.out.println();
        }
        
        userInput.close();
    }
}

Upvotes: 1

Views: 269

Answers (2)

arch2be
arch2be

Reputation: 306

Scanner can't be used for writing purposes, only reading. I like to use a BufferedWriter to write to text files.

out.write("some text"); //That's how you write text to the file.

out.newLine(); //That's how you made new line for your text.

Remember that you in this case, you should first create this file on your computer ex. text.txt and after that you must replace "path/filename.txt" with particular path to this file ex. "C:\\text.txt", more information you can find there: link

Working code bellow:

public static void main(String[] args) {
    double[] store = new double[5];

    try(Scanner userInput = new Scanner(System.in);
        BufferedWriter out = new BufferedWriter(new FileWriter("path/filename.txt"));) {

        for(int i = 0; i < 5; i++) {
            System.out.printf("Enter today\'s sales for store %d: ", i + 1);
            out.write("Enter today\'s sales for store " + (i + 1) +": ");
            store[i] = userInput.nextDouble();
            out.write(String.valueOf(store[i]));
            out.newLine();
        }

        System.out.println("\nSALES BAR CHART");
        out.write("\nSALES BAR CHART");
        out.newLine();

        for(int i = 0; i < 5; i++) {
            System.out.printf("Store %d: ", i + 1);
            out.write("Store " + (i + 1) + ": ");

            for(int c = 0; c < store[i]/100; c++) {
                System.out.print("*");
                out.write("*");
            }

            out.newLine();
            System.out.println();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Additionally I used try-with-resources. It's a better approach to closed resources. Here is a documentation about this mechanism: link

Upvotes: 1

user14971157
user14971157

Reputation:

Most operation system allow redirecting output to file:

% java BaseChart > file.txt

or using tee, output can be shown and saved to file:

% java BaseChart | tee file.txt

if this must be done in Java, there are some alternatives:

1 - create a PrintWriter, BufferedWriter or any Writer to write to a file, add print statements similar to the System.out.print ones. This site should have answers about how to do it.

2 - extend a FilterOutputStream to write to a file and to the original System.out, and set the filter as new System.out (this is doing the same job as tee). Also lots of post on StackOverflow about FilterOutputStream
this is mostly indicated for bigger projects with lots of output

...

Upvotes: 1

Related Questions