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.
/* 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.