0

im creating a program called "Book" for school and im having alot of trouble. IM suppost to find out how many times the character "a" comes up in a txt file. The txt file reads the following "go to the bathroom he said and he was right I needed to go to the bathroom" . Here is my code but it doesnt seem to work at all and i am stuck.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Book 
{

public static void main(String[] args)  throws FileNotFoundException
{

    Scanner text = new Scanner (new File("data.txt"));
    String word = null;
    int count = 0;
    while(text.hasNextLine())
    {
        word = text.nextLine();
        for (int i = 0; i < word.length(); i++)
        {
            if (word.substring(i) == "a")
            {
                count++;
            }
        }

    }


    System.out.print(count);
}
}
Not a bug
  • 4,286
  • 2
  • 40
  • 80

4 Answers4

2

The substring with one parameter returns a substring that starts at the given index. Also, you do not generally compare strings in Java using ==.

You need single quotes around a to make it a character constant, and charAt to get the specific character of the string, like this:

if (word.charAt(i) == 'a')
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @user3155726 You are welcome! (and welcome to the site). You may want to do some more testing to see if the answer has worked for you. If you are satisfied with the answer, consider accepting it by clicking the grey check mark next to it (you would be able to do it in about eight minutes). This would indicate to other site visitors that you are no longer actively looking for an improved answer, and earn you a badge on Stack Overflow. Good luck with your project! – Sergey Kalinichenko Jan 03 '14 at 02:55
0

I think you are looking the charAt() function. substring() returns a String, but you really want a Character. Note: Characters are denoted with single quotes '

if (word.charAt(i) == 'a')
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

word.substring(i) returns a String, when used in an equality check with ==, the String objects in the operand are compared based on their location in memory, rather than their values.

Additionally, word.substring(i) will return the entire string beginning at i, not the character at i. To return just the character, you'll need to also specify the end index. (see the docs)

This will probably work if you replace

if (word.substring(i) == "a")

with

if (word.substring(i, i+1).equals("a"))
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
0

You are reading your text file line by line but You can use FileInputStream to read char from file. Here is an example given.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    File file = new File("data.txt");
    if (!file.exists()) {
      System.out.println(args[0] + " does not exist.");
      return;
    }

    try {
      FileInputStream fis = new FileInputStream(file);
      char current;
      int count = 0;
      while (fis.available() > 0) {
        current = (char) fis.read();
        if (current == 'a') {
              count++;
         }
      }
      System.out.println(count);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Here you can iterate each char of file instead of line. Hope this will help you.

Not a bug
  • 4,286
  • 2
  • 40
  • 80