Java Counting Words and Characters - java

I'm supposed to create a program that counts the number of words and the average number of characters(without spaces) from a text file. I've created the program I'm just having one problem. My total number of characters counters counts the characters "-" and "." when counting words, I don't want this to happen. Currently I'm getting 300 characters counted. There are four "." and one "-" that should be removed from the counter so I can get the total value of 295. I tried using char but I get stuck with errors that don't allow me to compare string to char. My friend recommended I compare I should try to incorporate a way to compare char to Unicode but I don't know how to begin to code that.
public class Count {
public static void main(String[] args) {
File sFile = new File("s.txt");
FileReader in;
BufferedReader readFile;
String sourceCode;
int amountOfWords = 0;
int amountOfChars = 0;
try {
in = new FileReader(sFile);
readFile = new BufferedReader(in);
while ((sourceCode = readFile.readLine()) != null) {
String[] words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}
System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));
readFile.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("File does not exist or could not be found.");
System.err.println("FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
}
}

Before doing
String[] words = sourceCode.split(" ");
get rid of all the chars you do not want using replace
e.g.
sourceCode = sourceCode.replace ("-", "").replace (".", "");
String[] words = sourceCode.split(" ");

Or you could use all 3 characters "","." and ";" in the spilt I guess.

Here is the answer to your question...
public static int numberOfWords(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
num=num+l.length;
} else {
num++;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static int numberOfChars(File someFile) {
int num=0;
try {
String line=null;
FileReader filereader=new FileReader(someFile);
try (BufferedReader bf = new BufferedReader(filereader)) {
while((line=bf.readLine()) !=null) {
if (!line.equals("\n")) {
if (line.contains(" ") {
String[] l=line.split(" ");
for (String s : l) {
s=replaceAllString(s, "-", "");
s=replaceAllString(s, ".", "");
String[] sp=s.split("");
num=num+sp.length;
}
} else {
line=replaceAllString(line, "-", "");
line=replaceAllString(line, ".", "");
String[] sp=line.split("");
num=num+sp.length;
}
}
}
}
} catch (IOException e) {}
return num;
}
public static String replaceAllString(String s, String c, String d){
String temp = s.replace(c ,d);
return temp;
}

Related

Write a program NumberCount that counts the numbers (including integers and floating point values) in one or more text files. (Due today lol)

INSTRUCTIONS:
Write a program NumberCount that counts the numbers (including integers and floating point values) in
one or more text files. Note that only numbers separated by whitespace characters are counted, i.e., only
those numbers that can be read by either readInt() or readDouble() are considered.
So iv been trying to get this program to read text files and the title is pretty much the instructions but it does not want to read my textfiles that i have in the project folder (i tried moving it a bunch of times but anywhere i put it it didnt load up) This is my code
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class NumberCount implements Runnable {
private static int combinedCount = 0;
public static synchronized int getCombinedCount() {
return combinedCount;
}
public synchronized void setCombinedCount( int combinedCount) {
this.combinedCount = combinedCount;
}
String filename;
NumberCount(String filename) {
this.filename = filename;
}
NumberCount() {
}
#Override
public void run() {
String fileText = this.getTextFromFile(filename);
System.out.println(filename + ": " + countNumbers(fileText));
setCombinedCount(getCombinedCount() + countNumbers(fileText));
}
String getTextFromFile(String filename) {
try {
String data = "";
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
String st;
while ((st = br.readLine()) !=null) {
data += "\n" + st;
}
data = data.replaceAll("\n", " ");
return data;
} catch(Exception e) {
System.out.println("Unable to retrieve text from file: " + filename );
return "";
}
}
int countNumbers(String text) {
int count = 0;
String words[] = text.split(" ");
for (String word : words) {
try {
Integer.parseInt(word);
Float.parseFloat(word);
count++;
} catch (Exception e) {
}
}
return count;
}
String helpMessage() {
String data = "Please call as NumberCount <list of file names>\n";
data += "File names to be present in the same directory";
return data;
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println(new NumberCount().helpMessage());
} else {
Thread threads[] = new Thread[args.length];
int i = 0;
for (String filename : args) {
NumberCount nc = new NumberCount(filename);
threads[i] = new Thread(nc);
threads[i++].start();
}
try {
for(Thread t : threads) {
t.join();
}
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("combined count: " + getCombinedCount());
}
}
}

Search for multiline String in a text file

I have a text file from which i am trying to search for a String which has multiple lines. A single string i am able to search but i need multi line string to be searched.
I have tried to search for single line which is working fine.
public static void main(String[] args) throws IOException
{
File f1=new File("D:\\Test\\test.txt");
String[] words=null;
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String s;
String input="line one";
// here i want to search for multilines as single string like
// String input ="line one"+
// "line two";
int count=0;
while((s=br.readLine())!=null)
{
words=s.split("\n");
for (String word : words)
{
if (word.equals(input))
{
count++;
}
}
}
if(count!=0)
{
System.out.println("The given String "+input+ " is present for "+count+ " times ");
}
else
{
System.out.println("The given word is not present in the file");
}
fr.close();
}
And below are the file contents.
line one
line two
line three
line four
Use the StringBuilder for that, read every line from file and append them to StringBuilder with lineSeparator
StringBuilder lineInFile = new StringBuilder();
while((s=br.readLine()) != null){
lineInFile.append(s).append(System.lineSeparator());
}
Now check the searchString in lineInFile by using contains
StringBuilder searchString = new StringBuilder();
builder1.append("line one");
builder1.append(System.lineSeparator());
builder1.append("line two");
System.out.println(lineInFile.toString().contains(searchString));
More complicated solution from default C (code is based on code from book «The C programming language» )
final String searchFor = "Ich reiß der Puppe den Kopf ab\n" +
"Ja, ich reiß' ich der Puppe den Kopf ab";
int found = 0;
try {
String fileContent = new String(Files.readAllBytes(
new File("puppe-text").toPath()
));
int i, j, k;
for (i = 0; i < fileContent.length(); i++) {
for (k = i, j = 0; (fileContent.charAt(k++) == searchFor.charAt(j++)) && (j < searchFor.length());) {
// nothig
}
if (j == searchFor.length()) {
++found;
}
}
} catch (IOException ignore) {}
System.out.println(found);
Why don't you just normalize all the lines in the file to one string variable and then just count the number of occurrences of the input in the file. I have used Regex to count the occurrences but can be done in any custom way you find suitable.
public static void main(String[] args) throws IOException
{
File f1=new File("test.txt");
String[] words=null;
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String s;
String input="line one line two";
// here i want to search for multilines as single string like
// String input ="line one"+
// "line two";
int count=0;
String fileStr = "";
while((s=br.readLine())!=null)
{
// Normalizing the whole file to be stored in one single variable
fileStr += s + " ";
}
// Now count the occurences
Pattern p = Pattern.compile(input);
Matcher m = p.matcher(fileStr);
while (m.find()) {
count++;
}
System.out.println(count);
fr.close();
}
Use StringBuilder class for efficient string concatenation.
Try with Scanner.findWithinHorizon()
String pathToFile = "/home/user/lines.txt";
String s1 = "line two";
String s2 = "line three";
String pattern = String.join(System.lineSeparator(), s1, s2);
int count = 0;
try (Scanner scanner = new Scanner(new FileInputStream(pathToFile))) {
while (scanner.hasNext()) {
String withinHorizon = scanner.findWithinHorizon(pattern, pattern.length());
if (withinHorizon != null) {
count++;
} else {
scanner.nextLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(count);
Try This,
public static void main(String[] args) throws IOException {
File f1 = new File("./src/test/test.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String input = "line one";
int count = 0;
String line;
while ((line = br.readLine()) != null) {
if (line.contains(input)) {
count++;
}
}
if (count != 0) {
System.out.println("The given String " + input + " is present for " + count + " times ");
} else {
System.out.println("The given word is not present in the file");
}
fr.close();
}

Assigning the contents of a file to an variable

I'm currently working on an assignment for my intro programming class and I need to be able to assign the contents of a file to variable. The file contains a text paragraph. I need to be able to eventually count all characters and words in the text file.
What I have so far:
import java.io.*;
import java.util.Scanner;
public class CountWords {
public static void main(String[] args) throws IOException {
File inFile = new File("CountWordsTestFile.txt");
Scanner input = new Scanner(inFile);
String text = input.nextLine();
int words = 0, cha = text.length(), character = 0;
System.out.println(text);
System.out.println("The file contains");
System.out.println(words + " words");
System.out.println((cha + 1) + " characters");
input.close();
}
}
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void readWords(String filePath) {
int words = 0;
int characters = 0;
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(filePath);
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if (line.trim().replaceAll(" +", " ").length() == 0) continue;
characters += line.length();
line = replaceAllSpecialCharacters(line);
words += line.split("\\s").length;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Words : " + words);
System.out.println("Characters : " + characters);
}
public static String replaceAllSpecialCharacters(String line) {
return line.replaceAll(" +", " ").replaceAll("[^\\p{L}\\p{Z}]", " ").trim();
}
public static void main(String[] a) {
readWords("CountWordsTestFile.txt");
}
}
Edit :
Simple Code as your request
package test;
import java.io.*;
import java.util.Scanner;
public class ReadFile {
public static void readWords(String filePath) {
int words=0,characters=0;
Scanner in= null;
try {
in = new Scanner(new File(filePath));
while (in.hasNextLine()){
//Here you have the value line by line
String line =in.nextLine();
//if line is empty or has only spaces continue to the another line
if(line==null||line.trim().length()==0)continue;
//add the length for each line to compute characters
characters+=line.length();
//replace multiple spaces with one space
line=line.replaceAll(" +"," ");
//trim the line to remove spaces
line=line.trim();
//split the line with space to get array of words
String []wordsArray=line.split("\\s");
//the length of this array is the # of words in this line
words+=wordsArray.length;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Words : " + words);
System.out.println("Characters : " + characters);
}
public static void main(String[] a) {
readWords("CountWordsTestFile.txt");
}
}
Here is simplified version of Java program to count characters and words in a file. I assume here that the file is a text file and words in the file are separated by one or more spaces only, not by any delimiter.
public class CountWords {
public static void main(String[] args) {
//Initializing counter for words and characters in the file
int words = 0, chars = 0;
File inFile = new File("CountWordsTestFile.txt");
try {
Scanner scanner = new Scanner(inFile);
while(scanner.hasNextLine()){
//Read the line and assign it to a string
String str = scanner.nextLine();
//Increase the counter for no of characters
chars = chars + str.length();
//Split the line by spaces and store in an array
String[] strArr = str.split("[ ]+");
//Increase the counter for no of words
words = words + strArr.length;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("The file contains");
System.out.println(words + " words");
System.out.println(chars + " characters");
}
}
Hope it fits your requirement.

How do I provide null to BufferedReader's readLine() method using Eclipse

public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
String line;
try {
while( (line = in.readLine()) != null ) {
System.out.println(line);
lineCount++;
charCount += line.length();
String[] words = line.split("\\W");
wordCount += words.length;
}
System.out.println("charCount = " + charCount);
System.out.println("wordCount = " + wordCount);
System.out.println("lineCount = " + lineCount);
} catch (IOException e) {
e.printStackTrace();
}
}
Straight to the point: How do I exit the above while-loop? I've read on another question that readLine() returns null when there is no more line left to read, but how do I do that using Eclipse's console?
The only way I can manage to break the loop is to add in cases, such as
if(line.length() == 2 || line.equals("exit")))
break;
if you just want to read a line, why do you need while loop?
or if you want multiple inputs to be taken then you should specify how many inputs are to be taken in first readline();
otherwise i hope below code will resolve your issue :)
public class TestRandomArray {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try {
String line = in.readLine();
lineCount++;
charCount += line.length();
String[] words = line.split(" ");
wordCount += words.length;
System.out.println("charCount = " + charCount);
System.out.println("wordCount = " + wordCount);
System.out.println("lineCount = " + lineCount);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

java assign next() to string or break into characters

I am trying to create a program which will read a file and check whether the text is a palindrome or not. The code compiles, but doesnt really work.
The problem is I dont know how to break a complete token into characters or assign it to a string in order to use string's length to push(enqueue) each letter or digit into the stack(queue). Can anyone suggest a solution for this?
public static void main(String [] args) throws IOException{
StackReferenceBased stack = new StackReferenceBased();
QueueReferenceBased queue = new QueueReferenceBased();
Scanner s = null;
String fileName=args[0]+".txt";
int symbols = 0;
int lettersAndDigits =0;
int matches = 0;
try{
s = new Scanner(new File(fileName));
while(s.hasNext()){
String current = s.next();
for(int i=0;i<current.length();i++){
char temp = s.next().charAt(i);
if(Character.isLetterOrDigit(temp)){
stack.push(temp);
queue.enqueue(temp);
lettersAndDigits++;
}
else {
symbols++;
}
}
}
System.out.println("There are: " + " "+ symbols + " " +"symbols and " + " "+lettersAndDigits + " "+ "digits/letters");
}
catch (FileNotFoundException e) {
System.out.println("Could not open the file:" + args[0]);
} //catch (Exception e) {
//System.out.println("ERROR copying file");
finally {
if(s != null){
s.close();
}
}
while (!stack.isEmpty()){
if(!stack.pop().equals(queue.dequeue())){
System.out.println("not pali");
break;
}
else {
++matches;
}
}
if(matches==lettersAndDigits){
System.out.print("pali");
}
}
Instead of
char temp = s.next().charAt(i);
you need
char temp = current.charAt(i);
By calling s.next() you read the next token from the file and try to access the ith element of that token based on the first string's (current) length, which will lead to exceptions if the tokens read are shorter than the first stoken

Categories

Resources