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?
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 theScanner
created with a single argument should use the standard platform encoding... In addition, I agree with all points raised in your reply.– mgibsonbr
@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.– Victor Stafusa
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?).
– mgibsonbr
@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
– Victor Stafusa