Hello. I tried to write the uppercase file converter, but I gotconfused. Please
ID: 3613793 • Letter: H
Question
Hello. I tried to write the uppercase file converter, but I gotconfused. Please help me. Thank you!!Write a program that ask a the user for the names of two files. Thefirst file should be opened for reading and the second file shouldbe opened for writing. The program should read the contents of thefirst file, change all characters to uppercase, and store theresults in the second file. The second file will be a copy of thefirst file, except that all the characters will be uppercase. UseNotepad or another text editor to create a simple file that can beused to test the program.
This is what I did...
import java.util.Scanner; //Needed for the Scanner class
import java.io.*; // Needed for file classes
public class Uppercase
{
public static void main(String[] args) throwsIOException
{
String filename;
String filename2;
String writing;
// Create a Scanner objectfor keyboard input.
Scanner keyboard = newScanner(System.in);
// Get the filename.
System.out.print("Enter thefirst filename: ");
filename =keyboard.nextLine();
// Open the file.
FileReader freader = newFileReader(filename);
BufferedReader inputFile =new BufferedReader(freader);
// Read
writing =inputFile.readLine();
while (writing != null)
{
System.out.println(writing);
writing =inputFile.readLine();
}
System.out.print("Enter thesecond filename: ");
filename2 =keyboard.nextLine();
FileWriter fwriter = newFileWriter(filename2);
PrintWriter outputFile = newPrintWriter(fwriter);
System.out.println(writing.toUpperCase());
outputFile.println(writing);
outputFile.close();
inputFile.close();
}
}
Explanation / Answer
You need to write each line after you read it in. importjava.util.Scanner; // Needed for the Scanner class import java.io.*; // Needed for fileclasses public class Uppercase { publicstatic void main(String[]args) throws IOException { String filename; String filename2; String writing; //Create a Scanner object for keyboard input. Scanner keyboard =new Scanner(System.in); //Get the filename. System.out.print("Enter thefirst filename: "); filename = keyboard.nextLine(); //get the second filname System.out.print("Enter thesecond filename: "); filename2 = keyboard.nextLine(); //Open the file. FileReader freader =new FileReader(filename); BufferedReader inputFile= newBufferedReader(freader); //open output file FileWriter fwriter= newFileWriter(filename2); PrintWriter outputFile = new PrintWriter(fwriter); //Read writing = inputFile.readLine(); while (writing != null) { System.out.println(writing); writing = inputFile.readLine(); // write System.out.println(writing.toUpperCase()); outputFile.println(writing); } //close IO streams outputFile.close(); inputFile.close(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.