No string in an array with Bufferedreader

Asked

Viewed 70 times

3

I am developing an application that allows the user to enter two cities and then check an array with the existing "Cities", if those that the user enters exist.

The problem is when I try to read the city the user wants to look for with the Bufferedreader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Cidade1:");
    String cidade1 = "Santo Tirso";//br.readLine();
    System.out.println("Cidade2:");
    String cidade2 ="Felgueiras";// br.readLine();
**//desta maneira funciona, mas se colocar o readLine já nao encontra as cidades**

it does not find the cities, but if put as the code above, without asking the user to enter data then it finds.

Below, I show the complete method:

public void menuCidades() throws IOException {
    System.out.println("Introduza as cidades:\n");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Cidade1:");
    String cidade1 = "Santo Tirso";//br.readLine();
    System.out.println("Cidade2:");
    String cidade2 ="Felgueiras";// br.readLine();

    int cidade1w = getIDCidade((T) cidade1);
    int cidade2w = getIDCidade((T) cidade2);

    if (cidade1w != -1 && cidade2w != -1) {
        System.out.println("teste de entrar");
        // calcularPercursos_Parametros((T) cidade1w, (T) cidade2w);
    }

I’ve tried the Debugger, but I don’t understand what the problem is! There must be some beginner’s error and I’m not able to identify!

EDIT

Method of getIdCity:

 public int getIDCidade(T cidade) {
    int id = -1;
    String cidadec = (String) cidade;
    for (int i = 0; i < this.conjuntoCidades.length; i++) {

        if (this.conjuntoCidades[i] == cidadec) {
            id = i;

        }
    }
    return id;
}

Debuug with Buffer: Aqui mostro o debugg com o Buffer, as cidades estão iguais

  • What is that cast (T) in the getIDCity method? How are you comparing (checking if they are in the array) cities? If it is with equals or == you have to enter the name of the city EXACTLY as it is in the array.

  • @Genos added this method for you to see the implementation

1 answer

1


Use the equals method to compare these objects.

Your method would look like this.

public int getIDCidade(T cidade) {
    int id = -1;
    String cidadec = (String) cidade;
    for (int i = 0; i < this.conjuntoCidades.length; i++) {

        if (this.conjuntoCidades[i].equals(cidadec)) {
            id = i;

        }
    }
    return id;
}

I believe you are populating the inline Cities set with Estatic strings, which is why == works when the city name is placed directly in the source code, java is smart enough not to create two static String objects with the same value, with this when you put two "Santo Tirso" in the source code java creates only one object in memory making the == return true.

Take a look at this topic: https://stackoverflow.com/questions/3801343/what-is-string-pool-in-java

  • thanks for your help, Henrique!

Browser other questions tagged

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