Question

In: Computer Science

Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters...

Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters and lines contained in the file. For example, the output should be as follows:

Lines: 5 Chars: 124

Note: Write a main() method to test in the FileCounter.java. The file path should use relative path. Must use the provided text file text1.txt.

The text file say

This is an example 1
This is an example 1 2.
This is an example 1 2 3.
This is an example 1 2 3 4.
This is an example 1 2 3 4 5.

2. Suppose you have a binary file geo.dat (see provided data file) of data for a geological survey, such that each record consists of a longitude, a latitude, and an amount of rainfall, all represented by double. Write a program GeologicalData.java to read this file’s data and print them on the screen, one record per line.

Note: Write a main() method to test the code in the GeologicalData.java. The file path should use relative path and the actually file path should be passed into the method getData(String fileName) in the main() method. Must use the provided file geo.dat which has already contained the following data.

-143.78914479979002 59.482245058443084 17.645846630300042 -117.80303714838257 -25.94559103704657 4.513879725440151 49.63942128783603 129.1664680590585 6.825838082783708 -79.66606860669573 -18.800024954102696 9.501515919083086 62.82522223390336 -60.409595996464006 2.6984128008196984

The partial Coding is

public class FileCounter {

}

The other Coding


public class GeologicalData {
public static void getData(String fileName){

}
}

Solutions

Expert Solution

(1.)

Java Program:

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

public class FileCounter
{
        public static void main(String[] args) throws FileNotFoundException {
        
        String fileName = "text1.txt";
        int character_count = 0, line_count = 0;
        
        Scanner in = new Scanner(new File(fileName));
        
        while (in.hasNextLine()) {
            String line = in.nextLine();
            character_count += line.length();
            line_count += 1;
        }
        
        System.out.println("Lines : " + line_count + " Chars : " + character_count);
        
    }
}

text1.txt File

This is an example 1
This is an example 1 2.
This is an example 1 2 3.
This is an example 1 2 3 4.
This is an example 1 2 3 4 5.

Output:

(2.)

Java Program:

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

public class GeologicalData
{
        public static void main(String[] args) throws FileNotFoundException {
        
        String fileName = "geo.dat";
        getData(fileName);
        
    }
    
    public static void getData(String fileName) throws FileNotFoundException {
        
        int character_count = 0, line_count = 0;
        
        Scanner in = new Scanner(new File(fileName));
        
        System.out.println("Longitude            Latitude          Amount of rainfall");
        System.out.println("===========================================================");
        
        while (in.hasNextLine()) {
            String line = in.nextLine();
            System.out.println(line);
        }
    }
}

geo.dat File

-143.78914479979002 59.482245058443084 17.645846630300042
-117.80303714838257 -25.94559103704657 4.513879725440151
49.63942128783603 129.1664680590585 6.825838082783708
-79.66606860669573 -18.800024954102696 9.501515919083086
62.82522223390336 -60.409595996464006 2.6984128008196984

Output:

Thumbs Up Please !!!

THANK YOU!! PLEASE VOTE


Related Solutions

Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
Write a C program that reads a file and reports how many lines, words, and characters...
Write a C program that reads a file and reports how many lines, words, and characters appear in it. For the purpose of this program, a word consists of a consecutive sequence of any characters except white space characters. For example, if the file lear.txt contains the following passage from King Lear, Poor naked wretches, wheresoe’er you are, That bide the pelting of this pitiless storm, How shall your houseless haeds and unfed sides, Your loop’d and window’d raggedness, defend...
Write an assembly language program that reads move review information from a text file and reports...
Write an assembly language program that reads move review information from a text file and reports the overall scores for each movie as well as identifying the movie with the highest total score. There are four movie reviewers numbered from 1 to 4. They are submitting reviews for five movies, identified by the letters from “A” through “E”. Reviews are reported by using the letter identifying the movie, the review rating, which is a number from 0 to 100, and...
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT