How to add lines with name and date when converting file to upper case?

129 views Asked by At

I am trying to get my program to read input from a .txt file and then convert the input to uppercase letters.

/*
 * 2/16/24
 * Purpose of this program is to convert contents of a file to upper case letters.
 */


package UpperCaseFileConverter;
//import
import java.io.File;
import java.io.*;
import java.util.Scanner;

public class UpperCaseFileConverter {

    public static void main(String[] args) throws IOException 
    {    
        Scanner scanner=new Scanner(System.in);
        
        String userFileName;
          
        // get file name (my file name is going to be convert.txt for this example)
        System.out.print(" Enter the Filename: ");
        userFileName = scanner.nextLine();
               
        // open file        
        File file = new File(userFileName);
        
        while(!file.exists()) {
             System.out.print(userFileName+"does not exists. Please re-enter the file name\n"
             + "remember for this example we are using \"convert.txt\"" );
             userFileName = scanner.nextLine();
             file = new File(userFileName);
        }
        
        Scanner fileToScan = new Scanner(file);
        PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt");
        //Test file
        while(fileToScan.hasNext()) {
            System.out.println(fileToScan.nextLine().toUpperCase());
        //Write file to new .txt file   
        }
        while(fileToScan.hasNext()) {
        fileToWrite.println( fileToScan.nextLine().toUpperCase());
        }
        System.out.println("Contents have been converted to upper case and saved in convertToUpper.txt");
       fileToWrite.close();
       fileToScan.close();
        
    }
    

    }

I need the program to add a new line to the file that has my name, and then add to the end of the file the date, so the output should have 2 more lines than the input.

In my class and the text book for Java I simply have not been taught how to add to specific lines of a document, so I don't know how I would add to the first line... Either that or I'm and idiot and missed that part.

3

There are 3 answers

0
TheBigDookie On BEST ANSWER
/* xxxxx
 * 2/16/24
 * Purpose of this program is to convert contents of a file to upper case letters.
 */


package xxxxxUpperCaseFileConverter;
//Import
import java.io.*;
import java.util.Scanner;

public class xxxxxUpperCaseFileConverter {

    public static void main(String[] args) throws IOException 
    {  
        //Create scanner for keyboard input from user 
        Scanner scanner=new Scanner(System.in);
        
        //Create string name to hold user input
        String userFileName;
        
        //Get file name (my file name is going to be convert.txt for this example)
        System.out.print(" Enter the Filename: ");
            userFileName = scanner.nextLine();
               
        // Specify file        
        File file = new File(userFileName);
        
        //Error message
        while(!file.exists()) {
             System.out.print(userFileName+" does not exists. Please re-enter the file name\n"
             + "remember for this example we are using \"convert.txt\"" );
             userFileName = scanner.nextLine();
             file = new File(userFileName);
        }
        
        //Create output file and create a Scanner to read from the input file 
        PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt");  
        
        try (Scanner fileToScan = new Scanner(file)) {
            fileToWrite.println("XXXXX XXXXX");
            System.out.println("XXXXX XXXXX);
            while (fileToScan.hasNextLine()) {
                String line = fileToScan.nextLine();
                String upperCaseLine = line.toUpperCase();                
                fileToWrite.println(upperCaseLine);                
                //print the converted line to the console                
                System.out.println(upperCaseLine);                
            }
            fileToWrite.println("02,19,2024");
            System.out.println("02,19,2024");
        }

        //Confirm message
        System.out.println("Contents have been converted to upper case, Name and date have been added. New data saved in convertToUpper.txt");
        //Close input and output files.
       fileToWrite.close();      
        
    }
}

This seemed to work. It is based on the input from the other answers here. It writes output to a .txt document verified and data tested to console.

4
LHY On

I need the program to add a new line to the file that has my name, and then add to the end of the file the date, so the output should have 2 more lines than the input.

To do this, you can make use of fileToWrite to add the lines before (and after) you read the file contents (i.e. before the while loop).

fileToWrite.println("Name: John Doe");

// Your code to read and convert to upper case

String date = ...;
fileToWrite.println("Date: " + date);

Depending on what format you want, there are various ways to get the current date.


You should refer to Jacob Poland's answer to see if you have issues with your while loop logic for reading and converting to upper case.

2
g00se On

The answers so far don't seem to have covered what you were having difficulty with, i.e. placing your name and the date at the end of the converted file, although their comments are valid. I have not repeated their comments here, though I have edited some of your comments, in addition to adding some of my own. I didn't echo the rewrite to the console, but you can do that easily by reading the line into a String first as at least one of the other answers does:

/*
 * 2/16/24
 * Purpose of this program is to convert contents of a file to upper case letters.
 */

package uppercasefileconverter; // Package names are lower case in Java

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

import java.time.LocalDate;

public class UpperCaseFileConverter {

    public static void main(String[] args) throws IOException {
        final String NAME = "TheBigDookie";

        Scanner scanner = new Scanner(System.in);

        // get file name (my file name is going to be convert.txt for this example)
        System.out.print(" Enter the Filename: ");
        String userFileName = scanner.nextLine();

        // Specify file - NB this does not open the file
        File file = new File(userFileName);

        while (!file.exists()) {
            System.err.printf(
                    "%s does not exist. Please re-enter the file name%n and remember for this example we are using \"convert.txt\"",
                    userFileName);
            userFileName = scanner.nextLine();
            file = new File(userFileName);
        }

        try (Scanner fileToScan = new Scanner(file);
                PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt")) {
            while (fileToScan.hasNextLine()) {
                // Write file to new .txt file
                fileToWrite.println(fileToScan.nextLine().toUpperCase());
            }
            // Now write name and date
            fileToWrite.println(NAME);
            fileToWrite.println(LocalDate.now().toString());
        }
        System.out.println("Contents have been converted to upper case and saved in convertToUpper.txt");

    }
}