Question: Launch Eclipse, and create a new project named Lab_Project_7 . Add a new Main class to your project (with a main method). Download the file
Launch Eclipse, and create a new project named Lab_Project_7. Add a new Main class to your project (with a main method).
Download the file input7.txt into your project folder. Remember that if you created the project in the default location, the project folder is located in the workspace folder of your home folder (e.g.: C:\Users\doej1\workspace).
Copy and paste the highlighted code shown below into your main method.
Then, complete the program so that it
reads the first line of text from a user-specified text file as a String,
switches the two halves of the String, and
writes the modified String to a user-specified text file.
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { // 1. Declare and create a new Scanner variable named kbd for keyboard input // 2. Declare 3 String variables named inputFileName, outputFileName, and line // 3. Declare a File variable named inputFile // 4. Declare a Scanner variable named fileInput // 5. Declare a PrintWriter variable named fileOutput // 6. Declare 2 int variables named lineLength and middleIndex // 7. Using the kbd Scanner, get the inputFileName String as user input // 8. Create a new File object with the inputFileName, assign it to the inputFile variable // 9. Create a new Scanner object with the inputFile, assign it to the fileInput variable // 10. Using the fileInput Scanner, get the line String from the input file // HINT: use the Scanner nextLine method // 11. Set the lineLength variable to the line's length // HINT: use the String length method // 12. Set the middleIndex variable to the index of the line's middle character // 13. Reset the line variable to the String resulting from switching the 2 halves of the line // HINT: use the String length substring method and the String concatenation operator // 14. Using the kbd Scanner, get the outputFileName String as user input // 15. Create a new PrintWriter object with the outputFileName, assign it to the fileOutput variable // 16. Using the fileOutput PrintWriter, write the line String to the output file // HINT: use the PrintWriter println method // 17. Close the fileInput Scanner // 18. Close the fileOutput PrintWriter } // end main } // end class Main
Sample Run (user input in color):
Enter the input file name: input7.txt Enter the output file name: output7.txt
When your program execution is complete, open the output file. It should contain a single line:
reading text is to use the Scanner class.In Java, the most convenient mechanism for
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
