Question

Write a program in java that asks a user for a file name and prints the...

Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt):
This is a test
Hi there
Output on the console:
Number of characters: 22
Number of words: 6
Number of lines: 2
Data written to the file (Test1.txt):
tset a si sihT
ereth iH

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ReadFileCount.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;


public class ReadFileCount {

  
   public static void main(String[] args) throws FileNotFoundException {
           Scanner scan = new Scanner(System.in);
       System.out.println("Enter file name: ");
       String fileName = scan.next();
      
       StringBuffer finalsb = new StringBuffer();
       File file = new File(fileName);
       if(file.exists()){
           int numberOfLines = 0;
           int numberOfCharacters = 0;
           int numberOfWords = 0;
           Scanner scan1 = new Scanner(file);;
           while(scan1.hasNextLine()){
               StringBuffer sb = new StringBuffer();
              
               String s = scan1.nextLine();
               sb.append(s);
              
               numberOfLines++;
               numberOfCharacters = numberOfCharacters + s.length();
               String words[] = s.split(" ");
               numberOfWords = numberOfWords + words.length;
               finalsb.append(sb.reverse());
               finalsb.append("\n");
           }
           System.out.println("Number of characters: "+numberOfCharacters);
           System.out.println("Number of words: "+numberOfWords);
           System.out.println("Number of lines : "+numberOfLines);
           PrintStream ps =new PrintStream(file);
          
           ps.print(finalsb.toString());
           ps.flush();
           ps.close();
           System.out.println("Data written to the file "+fileName);
   }
       else{
           System.out.println("File does not exist");
       }
      
      
      
   }

}

Output:

Enter file name:
D:\\Test1.txt
Number of characters: 0
Number of words: 0
Number of lines : 0
Data written to the file D:\\Test1.txt

Test1.txt

tset a si sihT
ereht iH

Add a comment
Know the answer?
Add Answer to:
Write a program in java that asks a user for a file name and prints the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, write a program that asks the user for the name of a file. The...

    In Java, write a program that asks the user for the name of a file. The program should read all the numbers from the given file and display the total and average of all numbers in the following format (three decimal digits): Total: nnnnn.nnn Average: nnnnn.nnn Class name: FileTotalAndAverage

  • ***** JAVA ONLY ***** Write a program that asks the user to enter the name of...

    ***** JAVA ONLY ***** Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad to create a simple file that can be used to test the program. ***** JAVA ONLY *****

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • FiveOnly.java: Write a program that asks the user for the name of a file. The program...

    FiveOnly.java: Write a program that asks the user for the name of a file. The program should display only the first five lines of the file’s contents. If the file contains fewer than five lines, it should display the file’s entire contents. See Challenge 13 on p265. Hope this is enough. if you need the txtbook page let me know!

  • java Write a method named printEntireFile that prompts the user for a file name and points...

    java Write a method named printEntireFile that prompts the user for a file name and points the contents of that file to the console as output. You may assume that the file exists. For example. If the file example. txt contains the following input data: Then the following would be an example dialogue of your method:

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • In Java write a program that asks user’s name, records it in memory, prints the number...

    In Java write a program that asks user’s name, records it in memory, prints the number of times it saw the name since it was last started and goes back to asking user’s name.

  • C++ Write a program that asks for a number and then prints as many lines as...

    C++ Write a program that asks for a number and then prints as many lines as the user inputs. Each line contains as many pairs of characters ("*#") as the number of this line. It should look like the right half of a pyramid. Your version of the program must print the same result as the expected output. To to this lab, you must use two do-while loops. Two exceptions: When the user inputs a number less than 1, then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT