Question

Write a method in JAVA for accepting a text file as input and If the file...

Write a method in JAVA for accepting a text file as input and

If the file is of type .txt, then calculate and return the number of words in that txt file.

If the file is of type .csv, then return the number of rows and number of columns in that csv file.

If the file is of any other type, then return a message stating that file type is not supported.

Note: Use StringBuilder wherever applicable.

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


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class FileStats {
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter filename: ");
       String n = sc.next();
       System.out.println(getStats(n));
   }

   public static String getStats(String name) throws Exception {
       BufferedReader br = new BufferedReader(new FileReader(name));
       if (name.endsWith("txt")) {
           int wordCount = 0, lineCount = 0, charCount = 0;
           String line = br.readLine();
           while (line != null) {
               lineCount++;
               String arr[] = line.split("\\s");
               wordCount += arr.length;
               line = br.readLine();
           }
           return "The file " + name + " has words : " + wordCount;
       } else if (name.endsWith("csv")) {
           int cols = 0, rows = 0;
           String line = br.readLine();
           String arr[] = line.split(",");
           cols += arr.length;
           while (line != null) {
               rows++;
               line = br.readLine();
           }
           return "The file " + name + " has rows : " + rows + " and cols: " + cols;
       } else {
           return "file type is not supported";
       }
   }
}

<terminated > FileStats (2) [Java Application) C:\Progra Enter filename: input.txt The file input.txt has words : 10

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write a method in JAVA for accepting a text file as input and If the file...
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
  • Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method.

     Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • Lab Description : Exercise : a) Create an interface named ReadAndWriteFile, that has readAndReturnFile() and writeFile()...

    Lab Description : Exercise : a) Create an interface named ReadAndWriteFile, that has readAndReturnFile() and writeFile() methods. b) readAndReturnFile() and writeFile() both throws IOException, FileNotFoundException and Exception. c) Create a class named DealingWithTextFile that implements ReadAndWriteFile. This class read and write txt files. d) Create a class named DealingWithCsvFile that implements ReadAndWriteFile. This class read and write csv files. - Please mention that you have to deal with exceptions, use a dialog message to catch the exceptions. e) DealingWithTextFile class...

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

  • 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:

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • 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...

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