BoxWithADot
BoxWithADot

Reputation: 11

Counting a specific char in java, but my program only reads the first character in a word

I have a program that is supposed to count all the instances of a specific character, like 'A', in a specified file. I got it to count the characters, sort of, except it only looks at the character at the beginning of a word. So "a a aaa a ba" would only count as 4 "A"s and not 7. I've commented as best as I can so my train of thought is clear, but I'm fairly new to programming so I apologize in advance if I'm not being clear.

import java.util.Scanner;
import java.io.*;

public class Charcounter
{
public static void main(String[] args) throws IOException
{
    //accumulator
    int sum = 0;

    Scanner kb = new Scanner(System.in);

    //get filename and character to be counted from user
    System.out.println("Enter the name of a file: ");
    String filename = kb.nextLine();
    System.out.println("Enter the name of the character to be counted: ");
    char countedChar = kb.next().charAt(0);

    //check if file exists
    File file = new File(filename);
    if (!file.exists())
    {
        System.out.println("File specified not found.");
        System.exit(0);
    }

    //open file for reading
    Scanner inputFile = new Scanner(file);

    //read file and count number of specified characters
    while (inputFile.hasNext())
    {
        //read a char from the file
        char count = inputFile.next().charAt(0);

        //count the char if it is the one specified
        if (count == countedChar)
        {
             ++sum;
        }

    }

    //close file
    inputFile.close();

    //display number of the specified char
    System.out.println("The number of the character '" + countedChar + "' is : " + sum);
}
}

Upvotes: 0

Views: 997

Answers (6)

Fedy2
Fedy2

Reputation: 3207

As alternative you can use a Reader that is dedicated to char reading:

BufferedReader reader = new BufferedReader(new FileReader(file));

int read;

while((read = reader.read())>0) if (read==countedChar) count++;

reader.close();

Upvotes: 0

vidit
vidit

Reputation: 6451

It is because you are only comparing the first character.

 //read a char from the file
 // THIS : only the first character
 char count = inputFile.next().charAt(0);

 //count the char if it is the one specified
 if (count == countedChar)
 {
   ++sum;
 }

You should loop through all the characters and then increment the sum if it matches the countedChar, something like..

 String str = inputFile.next()
 for (int i = 0; i < str.length(); i++) {
   char count = str.charAt(i);   
   // check if it matches the countedChar and then increment.
 }

Upvotes: 2

belyjz
belyjz

Reputation: 187

while (inputFile.hasNext()) {
    String word = inputFile.next();
    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == countedChar) {
            ++sum;
        }
    }
}

Upvotes: 0

Pshemo
Pshemo

Reputation: 124215

It is because you are using

char count = inputFile.next().charAt(0);

By default next will read until it will find whitespace or end of data so now it reads and returns entire words.

If you want to make this approach work you need next to return only one character at a time so set delimiter in imputFile scanner to empty string like inputFile.useDelimiter("");.

Upvotes: 0

Showman
Showman

Reputation: 55

Try using a variable instead of zero which initializes as zero in the beginning of the class and then increment it for every char it counts, so:

   //read a char from the file
    char count = inputFile.next().charAt(m);
    ++m;
    //count the char if it is the one specified
    if (count == countedChar)
    {
         ++sum;
    }

And then in the main method define:

   int m = 0

Upvotes: 0

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

Thats because you are only reading the first char

    String word = inputFile.next();     //read a word
    int counter = 0; 

    for(counter=0;counter<word.length();counter++) // traverse through the word
    { 
        char count = word.charAt(i);       // ith char

        //count the char if it is the one specified
        if (count == countedChar)
        {
             ++sum;
        }
   }

Upvotes: 0

Related Questions