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;
}
What is that cast
(T)
in the getIDCity method? How are you comparing (checking if they are in the array) cities? If it is withequals
or==
you have to enter the name of the city EXACTLY as it is in the array.– Genos
@Genos added this method for you to see the implementation
– ZelDias