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 8484727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:27:26+00:00 2026-06-10T20:27:26+00:00

This is a homework assignment, I have to write a program that reads a

  • 0

This is a homework assignment, I have to write a program that reads a “input.txt” file and sees how many times to print a salutation (ex: Donald Duck, 4 so it prints Hello Donald Duck four times)
The problem I’m having is with outputting the salutation into a text file called “output.txt”
For some reason it only prints out the salutation for the last line in the input.txt instead of all of them.. Here’s what I have so far:

   import java.util.*;
   import java.io.*;

   public class Driver0 {

      public static void main (String [] args) {
         Scanner userInput = new Scanner(System.in);
         System.out.println("Would you like the output to be written to a file?");
         Scanner read;
         String input = userInput.nextLine(); //user input
         if(input.equals("yes")) {
            System.out.println("Writing to \"output.txt\" now..");
         }
         else {
            System.out.println("Printing to screen:"); 
         }        
            try {
            read = new Scanner(new File("input.txt")); //scanner reading file
            } 
            catch (FileNotFoundException e){
               System.out.println("File not found.");
               return;
            }
         while(read.hasNextLine()) {
            String newInput = read.nextLine();
            String [] inputArray = newInput.split(","); //spliting
            int num = 0;
            num = Integer.parseInt(inputArray[2].trim());
                for(int i = 0; i < num; i++) {
                if(input.equals("yes")) {  //writing to file 
                    Driver0.outputting(inputArray);
                  }
               else { //writing to screen
                  System.out.println("Hello" + inputArray[1] + inputArray[0]); 
               }
            } 
         }
      }
      public static void outputting (String [] inputArray) {
         PrintWriter output = null;
         String fileName = "output.txt";
            try {
            output = new PrintWriter(new FileWriter(fileName)); 
            } 
            catch(IOException error){
               System.out.println("Sorry, can't open for writing.");
               return;
            }
            output.println("Hello" + inputArray[1] + inputArray[0]);
                output.close();       
      } 
   }

My problem is most likely within the outputting method or where it has a comment that says writing to file next to it

  • 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-10T20:27:27+00:00Added an answer on June 10, 2026 at 8:27 pm

    The FileWriter class has a second constructor that takes a boolean argument to tell it to append to the end of the existing file rather than overwrite it from the start.

    output = new PrintWriter(new FileWriter(fileName, true));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions about this homework assignment: Write an assembly language program in
I have a homework assignment that I am supposed to write a http server
This is part of a homework assignment. What we have to do is write
I have to write this code for a homework assignment but I don't even
(this is indirectly a part of a much larger homework assignment) I have something
I have a homework assignment to sort an array in ascending order. Obviously, this
I have done a homework assignment, here is the problem statement: Your program should
For a homework assignment, I have to write a MySQL query to calculate the
This is a homework assignment, just for all that want to know. I'm writing
I have a homework assignment to write a multi-threaded sudoku solver, which finds all

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.