Error comparing strings

Asked

Viewed 482 times

1

I need to make a program that takes a sentence, then a letter, and returns 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");
        }

    }
}

But I’m getting the following error:

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

Which refers to that code block:

        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 whether the functions charAt() and equal() are being used correctly.

2 answers

4


The most performative and idiomatic form would be direct use with char both in the letter you want to search for and in the individual access. This is easier with o for` which automatically scans a data collection. If you keep calling a method in 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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1

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

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

for:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.