ANY HELP WITH THIS JAVA PROGRAM? PLEASE LOOK FOR THE NEEDED OUTPUT AT BOTTOM Nam
ID: 3816537 • Letter: A
Question
ANY HELP WITH THIS JAVA PROGRAM? PLEASE LOOK FOR THE NEEDED OUTPUT AT BOTTOM
Name your program ReadSoap.java and make sure the program includes ALL of the following:
1. A prompt for the user to input the file name to be read (i.e. soap.txt)
2. A check for the file’s existence prior to attempting to read lines in.
3. The appropriate construct so that the program continues to read lines until there are NO MORE lines found in the file.
4. A display of each line read on the console screen.
5. A file CLOSE at the end of the program code.
THIS IS THE CODE:
SHOULD READ THIS SOAP.TXT OUTPUT AFTER PRINTING PROCESS COMPLETED:
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
import java.io.*;
public class Readsoap
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
// Declare variables for
// (1)the name of the file to be opened and read
// (2) input that is read in from that file
// (3) a counter to increment as each line IS read
String fileName;
String line;
int count = 0;
// Code here to get the file name from the user.
System.out.print("Enter input file name: ");
fileName = keyboard.next();
//Code here to Check to see if file exists
File file = new File(fileName);
if(! file.exists()){
System.out.println(fileName+" name does not exist.");
keyboard.close();
return;
}
// Open the file.
Scanner fileScanner = new Scanner(file);
// Process the entire file one line at a time with an appropriate loop.
// Read a line, output that line to the console
while(fileScanner.hasNextLine()){
line = fileScanner.nextLine();
System.out.println(line);
count = count + 1;
}
// Close the file.
fileScanner.close();
keyboard.close();
// Exit the program
}
}
/*
sample run:
Enter input file name: soap.txt
This stuff is easy
You need to trust me
Even more than you
TRUST BAR SOAP!!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.