Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that asks the user the file name of an input file. If the f

ID: 3833808 • Letter: W

Question

Write a Java program that asks the user the file name of an input file. If the file doesn't exist, exit the program with an error message, otherwise, open the file. Ask the user enter another file name for the output file. If the file exists, display a message that the old content will be wiped out, if not, create a new file. Read in data line by line from the input file, convert all lowercase letters to uppercase letters and write to the output file. When done, output a message to screen displaying how many lines have been written.

Explanation / Answer

//This program takes a file as input, and will convert to either lowercase,
//or upper case, based on the user requirement specification, and writes the
//converted data to the output file again specified by the user.
import java.io.*;
import java.util.*;
class ConvertLetterCase
{
public static void main(String[] args)   throws FileNotFoundException
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.print("Please enter the input data file name: ");
String inFile = sc.next();
System.out.print("Please enter the output data file name: ");
String outFile = sc.next();
File fin = new File(inFile);
File fout = new File(outFile);
Scanner fileStream = new Scanner(fin);
PrintWriter pw = new PrintWriter(fout);
System.out.println("Choose an option:");
System.out.println("(A) Capitalize all words");
System.out.println("(B) Lowercase all words");
char choice = sc.next().charAt(0);
switch(choice)
{
case 'A':
           while(fileStream.hasNextLine())
           {
           String line = fileStream.nextLine();
           pw.write(line.toUpperCase() + " ");
           }
           pw.close();
           fileStream.close();
           break;
case 'B':
           while(fileStream.hasNextLine())
           {
           String line = fileStream.nextLine();
           pw.write(line.toLowerCase() + " ");
           }
           pw.close();
           fileStream.close();
           break;
}
System.out.print("Process another file? ");
char another = sc.next().charAt(0);
if(another != 'Y' && another != 'y')
return;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote