Can someone help me figure out why my word count is the same as my line count??
ID: 670658 • Letter: C
Question
Can someone help me figure out why my word count is the same as my line count??
It has to count the number of lines, words, and characters. Not including spaces or punctuation
My sample data.txt file is:
this is a test. 5 and 10.
This is a test.
------------------------------------------------------------------------------------------------------
import java.util.*;
import java.io.*;
public class wordMain{
public static void main(String[] args) {
try {
Scanner input = new Scanner(new FileReader("data.txt")); // file to read
int lineNum = 0;
int wordCount = 0;
int charCount = 0;
while (input.hasNextLine()) {
String line;
line = input.nextLine();
lineNum++;
line = line.replace(" ", "");
line = removeAllSpecialCharacters(line);
String str[] = line.split(("\s+")); // creates a array of words separated by space
for ( int i = 0; i <str.length ; i ++) {
if (str [i].length() > 0) {
wordCount ++;
}
}
charCount += (line.length());
}
System.out.println("Number of line(s): " + lineNum + " ");
System.out.println("Number of word(s): " + wordCount + " ");
System.out.println("Number of character(s): " + charCount);
input.close();
}
catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
private static String removeAllSpecialCharacters(String string)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < string.length(); i++)
{
char ch = string.charAt(i);
// Allow only letters, digits, and spaces.
if (Character.isLetterOrDigit(ch))
{
stringBuilder.append(ch);
}
}
return stringBuilder.toString();
}
}
Explanation / Answer
REMOVE the line:
line = line.replace(" ", "");
/////////////////////
import java.util.*;
import java.io.*;
public class wordMain{
public static void main(String[] args) {
try {
Scanner input = new Scanner(new FileReader("data.txt")); // file to read
int lineNum = 0;
int wordCount = 0;
int charCount = 0;
while (input.hasNextLine()) {
String line;
line = input.nextLine();
lineNum++;
line = removeAllSpecialCharacters(line);
String str[] = line.split(("\s+")); // creates a array of words separated by space
for ( int i = 0; i <str.length ; i ++) {
if (str [i].length() > 0) {
wordCount ++;
}
}
charCount += (line.length());
}
System.out.println("Number of line(s): " + lineNum + " ");
System.out.println("Number of word(s): " + wordCount + " ");
System.out.println("Number of character(s): " + charCount);
input.close();
}
catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
private static String removeAllSpecialCharacters(String string)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < string.length(); i++)
{
char ch = string.charAt(i);
// Allow only letters, digits, and spaces.
if (Character.isLetterOrDigit(ch))
{
stringBuilder.append(ch);
}
}
return stringBuilder.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.