We are going to do this exercise by writing the object that solves the problem f
ID: 3573619 • Letter: W
Question
We are going to do this exercise by writing the object that solves the problem first (in a source file called TicketSeller.java) and then testing it using code we write into DocStats.java. Using the techniques shown on the web page titled How to Start Every Project in this Class to create a source file called DocStats.java. This is where your code will go.
Testing Your Code
Your DocStats.java should contain code for your solution to this problem. Ask the user for the name of a file (using a scanner) and outputs the statistics. A sample run is shown below:
The file "goblin.txt" is part of the project from GitHub, and should be seen in your project in Eclipse. It must be located in root directory of the project.
Once you've written your code run the code by single clicking on DocStats.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?
(Goblin.txt)
Evening by evening
Among the brookside rushes,
Laura bowed her head to hear,
Lizzie veiled her blushes:
Crouching close together
In the cooling weather,
With clasping arms and cautioning lips,
With tingling cheeks and finger tips.
'Lie close,' Laura said,
Pricking up her golden head:
'We must not look at goblin men,
We must not buy their fruits:
Who knows upon what soil they fed
Their hungry thirsty roots?'
'Come buy,' call the goblins
Hobbling down the glen.
'Oh,' cried Lizzie, 'Laura, Laura,
You should not peep at goblin men.'
Lizzie covered up her eyes,
Covered close lest they should look;
Laura reared her glossy head,
And whispered like the restless brook:
'Look, Lizzie, look, Lizzie,
Down the glen tramp little men.
One hauls a basket,
One bears a plate,
One lugs a golden dish
Of many pounds weight.
How fair the vine must grow
Whose grapes are so luscious;
How warm the wind must blow
Through those fruit bushes.'
'No,' said Lizzie, 'No, no, no;
Their offers should not charm us,
Their evil gifts would harm us.'
She thrust a dimpled finger
In each ear, shut eyes and ran:
Curious Laura chose to linger
Wondering at each merchant man.
One had a cat's face,
One whisked a tail,
One tramped at a rat's pace,
One crawled like a snail,
One like a wombat prowled obtuse and furry,
One like a ratel tumbled hurry skurry.
She heard a voice like voice of doves
Cooing all together:
They sounded kind and full of loves
In the pleasant weather.
Explanation / Answer
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class DocStats {
public static void main(String[] args) throws IOException {
System.out.println("Enter the file name:");
String fileName=new Scanner(System.in).next();//read the file name from the user
FileReader fr= new FileReader(fileName);
int lineCount=0,wordCount=0,charCount=0;//counting variables for lines,words and characters
int c;
while((c=fr.read())!=-1){ //reading each character from the file
if((char)c==' '){ //'/n' depicts new line as well as new word
lineCount++;
wordCount++;
}
else if((char)c==' ') // a space character is used to count words .. because words are separated by words
wordCount++;
charCount++;
}
charCount-=lineCount;//since new line delimiter AKA '/n' can't be considered as a readable character
System.out.println("Number of characters:"+(charCount));
System.out.println("Number of words:"+(wordCount+1));
System.out.println("Number of lines:"+(lineCount+1));
}
}
Note: The file specified by the user should be present in the Root directory only
Output:
Enter the file name:goblin.txt
Number of characters:1450
Number of words:257
Number of lines:49
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.