Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:39:24+00:00 2026-06-15T22:39:24+00:00

I am writing a kind of a cash machine program that will output the

  • 0

I am writing a kind of a cash machine program that will output the data into a file (Yes, I know its not in English, but that’s not the point) and I am experiencing an error.

When I try to use PrintWriter it doesn’t work, I don’t know why.

    public void writeFooter(List<Purchase> list) throws Exception{

    int amountSold = 0;
    int amountNotSold = 0;

    int moneyRecieved = 0;

    openStreams();

    printer.println("Проданные товары: ");

    for(int i = 0; i <= list.size() - 1;i++){
        if(list.get(i).isSold()){
            writeToFile(list.get(i), dir);
            amountSold++;
            moneyRecieved += list.get(i).getPrice();
        }
    }
    printer.println();
    printer.println("Не проданные товары: ");
    for(int i = 0; i <= list.size() - 1; i ++){
        if(!list.get(i).isSold()){
            writeToFile(list.get(i), dir);
            amountNotSold++;
        }
    }

    printer.println();
    printer.println("Всего: "+list.size());
    printer.println("Кол-во проданно: "+ amountSold);
    printer.println("Кол-во не проданно: "+ amountNotSold);
    printer.println("Выручка: "+ moneyRecieved);

    printer.flush();

    System.out.print(printer.checkError());

    closeStreams();

}



private void openStreams() throws IOException{
    writer = new FileWriter(file,true);
    buffer = new BufferedWriter(writer);
    printer = new PrintWriter(buffer);
}
private void closeStreams() throws IOException{
    printer.flush();
    printer.close();
    buffer.close();
    writer.close();
}
    public void writeToFile(Purchase purchase,String dir) throws Exception{
    file = new File(dir);

    if(!file.exists()){
    file.createNewFile();
    }

    openStreams();

    printer.println(purchase.getName() + "   По цене:   " + purchase.getPrice() + "руб");

    closeStreams();
}

The for loops work, but the lines. It really confuses me!
I have tried checkError() and I get true, but that still doesn’t help.

Can someone please explain what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:39:25+00:00Added an answer on June 15, 2026 at 10:39 pm

    Why are you opening and closing stream in your writeToFile call ?
    Shouldn’t you open the streams in writeFooter() before the for loop, put a try block around the writing code, and close them in a finally section .

    The way I see it,
    You’ve openedstream first in the writeFooter, the you write printer.println(“Проданные товары: “);, and then your for loop’s first iteration calls writeToFile, will again open the non-closed stream, that will surely overwrite the first printed line.

    Opening and closing File for writing one line is way too inefficient.

    You need something like

    writeFooter(...) {
    
      try {
        openStreams();
        writeToFile();
      } finally {
        closeStream();
      }
    }
    

    Where writeToFile simply writes , doesn’t open or close stream, and closeStream safely closes the streams, i.e. check for null etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some kind of logger that produces encrypted log-file. Unfortunately, cryptography is not
Perl is really good for writing the kind of string/file parsing programs that I
I am writing a tkinter program that is kind of a program that is
I'm writing a brute-force program for enumerating a certain kind of integer sequence (not
i'm writing some kind of application that will send http requests to server. My
So I'm kind of new to this, I'm writing a WIN32 app that records
I am writing a program that executes a set of tasks sequentially. Each task
I'm trying to learn more about reading/writing to an XML-file (as a kind of
I am writing an app that will use Reverse Geocoding to translate the user's
I am writing an App that requires some kind of outside input which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.