Write a program that prompts the user for a file name


Exercise: Formatting Java files

Write a program that prompts the user for a file name, assuming that the file contains a Java program. Your program should read the file and print its contents properly indented. When you see a left-brace character { in the file, increase your indent level by four spaces. When you see a right-brace character } , decrease your indent 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, for, etc.) 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. Consider using a class constant for the number of spaces to indent (4) so that it can be easily changed.

  • You may start with theFormatJavaProgramprovided.
  • CHALLENGE: Don't use the providedFormatJavaProgram, which provides the logic for deciding when to indent. Instead, start your program from scratch, and try to write the logic yourself!
  • Output the newly formatted file as a new Java file called _Formatted.java. It will be up to the user to change the name appropriately for compilation later.
  • CHALLENGE 2: Save the formatted code to the same file that you read itfrom!
  • Use boilerplate code from program we developed today!
  • Usean "ugly" version of theHoursWorkedprogram we developed in class today as input to yourFormatJavaProgramprogram.
  • Makesure you comment your code well!

Solution Preview :

Prepared by a verified Expert
Business Economics: Write a program that prompts the user for a file name
Reference No:- TGS02234889

Now Priced at $30 (50% Discount)

Recommended (97%)

Rated (4.9/5)