Error in comparing strings

1

I need to do a program where the program receives a phrase and then a letter, and then return how many times the letter appears in the sentence. So I did this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercicio1_string {

    public static void main(String[] args) {
        InputStreamReader c = new InputStreamReader(System.in);
        BufferedReader cd = new BufferedReader(c);
        String frase = "";
        String letra = "";

        System.out.println("Escreva uma frase: ");
        try {
            frase = cd.readLine();
        } catch(IOException e) {
            System.out.println("Erro de entrada");
        }

        System.out.println("Escreva uma letra para encontrar na frase: ");
        try {
            letra = cd.readLine();
        } catch(IOException e) {
            System.out.println("Erro de entrada");
        }

        int contador = 0;
        for(int i = 0; i < frase.length(); i++) {
            if(frase.charAt(i).equals(letra)) {
                contador++;
            }
        }

        if(contador == 0) {
            System.out.println("Nao existe a letra na frase");
        } else {
            System.out.println("A letra aparece " + contador + " vezes");
        }

    }
}

However I'm getting the following error:

Exercicio1_string.java:37: error: char cannot be dereferenced
            if(frase.charAt(i).equals(letra)) {

That references this block of code:

int contador = 0;
        for(int i = 0; i < frase.length(); i++) {
            if(frase.charAt(i).equals(letra)) {
                contador++;
            }
        }

I would like to know the reason for the error and if the charAt() function and the equal() function is being used correctly?

    
asked by anonymous 07.09.2018 / 20:15

2 answers

3

The most performative and idiomatic form would be the direct use with char both in the letter that wants to search and in the individual access. This is made easier with o for 'which scans a collection of data automatically. If you get calling a method on a loop you will have a huge waste of processing.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Exercicio1_string {
    public static void main(String[] args) {
        InputStreamReader c = new InputStreamReader(System.in);
        BufferedReader cd = new BufferedReader(c);
        String frase = "";
        char letra = ' ';
        System.out.println("Escreva uma frase: ");
        try {
            frase = cd.readLine();
        } catch (IOException e) {
            System.out.println("Erro de entrada");
        }
        System.out.println("Escreva uma letra para encontrar na frase: ");
        try {
            letra = cd.readLine().charAt(0);
        } catch (IOException e) {
            System.out.println("Erro de entrada");
        }
        int contador = 0;
        for (int caractere : frase.toCharArray()) if (caractere == letra) contador++;
        if (contador == 0) System.out.println("Nao existe a letra na frase");
        else System.out.println("A letra aparece " + contador + " vezes");
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
07.09.2018 / 20:40
1

The charAt method returns the primitive type char , so it can not be referenced. You can replace the excerpt:

if(frase.charAt(i).equals(letra)) {
    contador++;
}

by:

if(frase.charAt(i) == letra.charAt(0)) {
    contador++;
}
    
07.09.2018 / 20:28