Write a program that prompts the user for a file name, the file contains a Java
ID: 3771601 • Letter: W
Question
Write a program that prompts the user for a file name, the file contains a Java program. Your program should read the file and print to a file its contents properly indented. When you see a left- brace character { in the file, increase your indentation level by four spaces. When you see a right- brace character }, decrease your indentation level by four spaces. You may assume that the file has only one opening or closing brace per line, that every block statement (such as if or for) uses braces rather than omitting them, and that every relevant occurrence of a { or } character in the file occurs at the end of a line. This means, you are using K&R style blocks, as in:
while (i < 0) { ...
}
Consider using a class constant for the number of spaces to indent (4), so that it can easily be changed later.
Explanation / Answer
import java.io.*;
public class TextPrint {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
if (line=='{')
{
System.out.println(" "+line);
}else if(line=='}'){
System.out.println(line+" ");
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" +fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '"+ fileName + "'");
ex.printStackTrace();
}
}
}
at the place of runtime input file name code as :
import java.io.*;
import java.util.*;
public class TextPrint {
public static void main(String [] args) {
// The name of the file to open.
Scanner sr = new Scanner(System.in);
//String fileName = "temp.txt";
String fileName;
// This will reference one line at a time
String line = null;
System.out.println("Enetr java file name to read:");
fileName = sr.next();
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
if (line=='{')
{
System.out.println(" "+line);
}else if(line=='}'){
System.out.println(line+" ");
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" +fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '"+ fileName + "'");
ex.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.