Good post this because even though I have solved it so that it looks good I do not understand why it gives me. I hope you can help me, I have the following code.
package com.example.ejercicio1;
import java.util.Scanner;
class Ejercicio1 {
int edad;
String nombre, apellidos;
Scanner teclado;
public void inicializar() {
teclado=new Scanner(System.in);
System.out.println("Ingrese nombre:");
nombre=teclado.next();
System.out.println("Ingrese sus apellidos");
apellidos=teclado.next();
System.out.println("Ingrese edad:");
edad = teclado.nextInt();
}
public void imprimir(){
System.out.println("El usuario se llama " + nombre + " " + apellidos);
}
public void esMayor(){
if(edad > 18){
System.out.println("El usuario es mayor de edad");
}else {
System.out.println("No cumple la edad mínima");
}
}
public static void main(String[] args) {
Ejercicio1 ejercicio1;
ejercicio1 = new Ejercicio1();
ejercicio1.inicializar();
ejercicio1.imprimir();
ejercicio1.esMayor();
}
}
And I get the following error Enter name: David Enter your last names Sales marin Enter age:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor (Scanner.java:864) at java.util.Scanner.next (Scanner.java:1485) at java.util.Scanner.nextInt (Scanner.java:2117) at java.util.Scanner.nextInt (Scanner.java:2076) at com.example.exercise1.Exercise1.initialize (Exercise1.java:19) at com.example.ejercicio1.Ejercicio1.main (Exercise1.java:39)
But if I change line 19 and 18 and put it on surnames, it's fine with me ... someone who knows why.
Thanks