Why am I not getting into the if Else of this code

Asked

Viewed 598 times

1

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Scanner;

    public class ControladorVeiculo {
        public static void main(String[] args) {
            ArrayList <Veiculo> veiculos = new ArrayList<>();
            while (true){
                System.out.println("Digite 1 para digitar um Veículo ou qualquer outro número para sair");
                Scanner entrada1 = new Scanner (System.in);
                int valorEntrada = entrada1.nextInt();

                if (valorEntrada == 1){
                    System.out.println("Digite PASSEIO para instanciar um carro " +
                             "de passeio ou CAMINHÃO para instanciar um caminhão ");
                    Scanner entrada2 = new Scanner (System.in);
                    String veiculoStr = entrada2.nextLine();
                    if(veiculoStr.equals("PASSEIO")){
                         CarroPasseio passeio = new CarroPasseio();
                        System.out.println("Digite o modelo do seu carro de passeio: ");
                         String modelo = entrada2.nextLine();
                         passeio.setModelo(modelo);
                         System.out.println("Digite a placa do seu carro: ");
                         String placa = entrada2.nextLine();
                         passeio.setPlaca(placa);
                         System.out.println("Digite a capacidade máxima de pessoas que o carro suporta: ");
                         int capacidadeMaxima = entrada2.nextInt();
                         passeio.setCapacidadeMaxima(capacidadeMaxima);
                         System.out.println("Digite a cor do seu carro: ");
                         String cor = entrada2.nextLine();
                         passeio.setCor(cor);
                         System.out.println("Digite o peso que seu carro suporta: ");
                         double pesoTotal = entrada2.nextDouble();
                         passeio.setPeso(pesoTotal);
                         veiculos.add(passeio);
                   }else if(veiculoStr.equals("CAMINHÃO")){
                       Caminhao novo = new Caminhao();
                            System.out.println("Digite o modelo do seu caminhão: ");
                            String modelo2 = entrada2.nextLine();
                            novo.setModelo(modelo2);
                            System.out.println("Digite a placa do seu caminhão: ");
                            String placa2 = entrada2.nextLine();
                            novo.setPlaca(placa2);
                            System.out.println("Digite a capacidade máxima de pessoas que o seu caminhão suporta: ");
                            int capacidadeMaxima2 = entrada2.nextInt();
                            novo.setCapacidadeMaxima(capacidadeMaxima2);
                            System.out.println("Digite a quantidade de carga máxima que seu caminhão suporta: ");
                            double cargaMaxima = entrada2.nextDouble();
                            novo.setCargaMaxima(cargaMaxima);
                            System.out.println("Digite a altura maxima do seu caminhão: ");
                            double alturaMaxima = entrada2.nextDouble();
                            novo.setAlturaMaxima(alturaMaxima);
                            System.out.println("Digite o comprimento do seu caminhão: ");
                            double comprimento = entrada2.nextDouble();
                            novo.setComprimento(comprimento);
                            veiculos.add(novo);
                   }else
                       break;

                }else
                    break;

               }
                   Iterator<Veiculo> it = veiculos.iterator();
                   while(it.hasNext()){
                       Veiculo v = it.next();
                       System.out.println(v.toString());
                       if(v instanceof CarroPasseio ){
                           System.out.println("É um veiculo de Passeio");
                       }
                       if(v instanceof Caminhao){
                           System.out.println("É um caminhão");
                       }


                   }

            }
        }

The error is execution, when I run and type "TRUCK" the program ends! How do I solve this? To be more specific to Else if he does not enter is what is on line 36 do not know why this is happening... When I took the À by truck(a)o and left only "CAMINHAO" worked, but why does it happen?

2 answers

3

Your problem is encoding of accents and the following test confirms it. Just put some System.out.println in their elses:

            } else {
                System.out.println("Voce digitou " + veiculoStr);
                break;
            }

        } else {
            System.out.println("Voce digitou " + valorEntrada);
            break;
        }

You will see that what appears when you type "TRUCK" is not exactly what it should be. the à will be exchanged for something else. This is what happens to me:

Digite 1 para digitar um Veículo ou qualquer outro número para sair
1
Digite PASSEIO para instanciar um carro de passeio ou CAMINHÃO para instanciar um caminhão 
CAMINHÃO
Voce digitou CAMINH�O

Okay, and how to fix it? Like this:

Scanner entrada2 = new Scanner(System.in, "windows-1252");

Or maybe so:

Scanner entrada2 = new Scanner(System.in, "ISO-8859-1");

Here’s the result on my machine (it’s the same with either of two ways):

Digite 1 para digitar um Veículo ou qualquer outro número para sair
1
Digite PASSEIO para instanciar um carro de passeio ou CAMINHÃO para instanciar um caminhão 
CAMINHÃO
Digite o modelo do seu caminhão: 

A few more tips:

  1. You should have posted some of the classes Veiculo, CarroPasseio and Caminhao. Fortunately recreating them with some getters and setters was not difficult.

  2. You should only wear one Scanner to read the input. It makes no sense to have two or more.

  3. In the loop while in the end, it would be easier to use the Enhanced-for so you don’t have to deal with Iterators.

  4. The encoding form "windows-1252" is hideous. Try to get rid of it if possible. Using "ISO-8859-1" is slightly less worse, but it’s still bad.

  • I think you’re right, but only the AP can confirm this: after all, that was the result you got in the your environment, nothing guarantees that the AP console also use windows-1252 (again, I think probably). Anyway, I find it strange, because the Scanner created with a single argument should use the standard platform encoding... In addition, I agree with all points raised in your reply.

  • 1

    @mgibsonbr I tested with ISO-8859-1 and it worked too. I had checked it before but it had gone wrong because I had put it on Scanner wrong inadvertently and I thought I had failed. I agree it’s super weird and when I tried to get the native encoding gave it that it was UTF-8. So I don’t know yet what to do to mitigate that detail and detect input encoding in a decent way.

  • Terminal work is always complicated... I’ve had similar problems, only in Python (Pypy) and not in Java. After all, it was a bug in the implementation... It may be that Java has similar problems, I do not know (it is not strange that when printing the accents on the screen it does correctly, but when reading from the keyboard not?).

  • 1

    @mgibsonbr This reading wrong and writing right is the most bizarre. This is also bizarre: http://stackoverflow.com/questions/23904391/scanner-vs-system-console-write

-1

Why are you using special characters. ~ it is not advisable to use for checks, certainly this function does not recognize the special characters. For example, in the language C to put accents has to put in Hex, so that’s certainly it. The function does not recognize accents... If you want to understand better use breakpoints and debug, you do a proper check.

Browser other questions tagged

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