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.
This seemed to work. It is based on the input from the other answers here. It writes output to a
.txtdocument verified and data tested to console.