Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Eclipse, create a class named Counter that has one String field called text.

ID: 3535176 • Letter: I

Question

In Eclipse, create a class named Counter that has one String field called text. It has a constructor that sets the one field from a parameter. It also has the following no-parameter methods: getCharCount that returns the number of characters in the text field, excluding ' ' (new line) and ' ' (tab) characters; getWordCount that returns the number of words in the text field, which are sequences of characters separated by new line, tab, space(s), period or comma; and getLineCount that returns the number of lines.

You may go to the API to find methods in the String class that may be helpful, or you could handle it directly.

Also create a Main class that has the normal main method that you need to start the program. The main method should ask the user for a string. Use Scanner to read in the line containing the string.

Furthermore, Ask the user for the name of a local input text file (.txt), and return an error message if it cannot find the file. The input file should be long and varied enough that you can test the Counter methods.

Using the string from the input file or the user response, your program should create a Counter object with a String parameter that has the contents of the input file. Then you print out the test string along with the various counts, including appropriate labeling of the numbers.

Explanation / Answer


public class Counter {
private String text;

public Counter(String text) {
super();
this.text = text;
}
public int getCharCount() {
int count =0;
for(int i=0; i < text.length(); i++) {
if(text.charAt(i) != ' ' && text.charAt(i) != ' ') {
count++;
}
}
return count++;
}
public int getWordCount() {
int count =1;
for(int i=0; i < text.length(); i++) {
if(text.charAt(i) == ' ' || text.charAt(i) == ' ' || text.charAt(i) == ' ' || text.charAt(i) == ',' || text.charAt(i) == '.') {
count++;
}
}
return count++;
}
public int getLineCount() {
int count =0;
boolean multiline = false;
for(int i=0; i < text.length(); i++) {
if(text.charAt(i) == ' ' ) {
count++;
multiline = true;
}
}
if(!multiline)
count = 1;
return count++;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;



public class Test {

public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
System.out.println("Enter a text string ");
String text = inputScanner.nextLine();
Counter counter = new Counter(text);
System.out.println("Char count: " + counter.getCharCount());
System.out.println("Word count: " + counter.getWordCount());
System.out.println("Line count: " + counter.getLineCount());


System.out.println("Enter a file name containing text ");
String fileName = inputScanner.next();
try {
Scanner fileScanner = new Scanner(new File(fileName));
String fileText ="";
while(fileScanner.hasNextLine()) {
fileText += fileScanner.nextLine() + " ";
}

Counter counter2 = new Counter(fileText);
System.out.println("From file");
System.out.println("Char count: " + counter2.getCharCount());
System.out.println("Word count: " + counter2.getWordCount());
System.out.println("Line count: " + counter2.getLineCount());
} catch (FileNotFoundException e) {
System.out.println("File not exists");
}


}

}
Output:


Enter a text string
This is a sample text to test counter
Char count: 37
Word count: 8
Line count:1
Enter a file name containing text
textfile.txt
From file
Char count: 91
Word count: 21
Line count: 4