Java does not respect the Scanner

0

I am having problems with a java program using classes. The problem is that when I run the program name, age and career are requested when I enter the name all right, when I enter the age and give it to enter to run only run the println but do not wait for the data to enter but which jumps to the next parameter.

I do not understand why he jumps and does not wait for him to give him the information.

My code is as follows:

package compararedades;

import java.util.Scanner;
/**
*
* @author ERICK UNITEC
*/
public class CompararEdades {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    nombre a1 = new nombre();

    edad e0 = new edad();

    carrera c1 = new carrera();

    System.out.println("Bienvenido");



    System.out.println("Ingrese el nombre del primer alumno");
    String n1 = sc.nextLine();
    a1.nombre1(n1);

    System.out.println("Ingrese la edad del primer alumno");
    int edad1 = sc.nextInt();
    e0.ed1(edad1);

    System.out.println("Ingrese la carrera");
    String carr1 = sc.nextLine();
    c1.establecercarrera1(carr1);

    System.out.println("Ingrese el nombre del segundo alumno");
    String n2 = sc.next();
    a1.nombre2(n2);

    System.out.println("Ingrese la edad del segundo alumno");
    int edad2 = sc.nextInt();
    e0.ed2(edad2);

    System.out.println("Ingrese la carrera");
    String carr2 = sc.nextLine();
    c1.establecercarrera2(carr2);

    if(e0.Obteneredad1()> e0.Obteneredad2()){

    System.out.printf("La edad de %s es mayor que la de %s",a1.obtenernombre1(),a1.obtenernombre2());

    }

    else{

    System.out.printf("La edad de %s es mayor que la de %s",a1.obtenernombre2(),a1.obtenernombre1());

    }






}

}

    
asked by Erick Finn 04.07.2018 в 00:54
source

1 answer

1

The problem is that unlike nextLine() , next(), nextInt(), nextDouble(), nextFloat(), ... reads the next token but does not consume the line break character \n that is generated when you press ENTER. For that reason, when you enter nextLine() after nextInt() , nextLine() consumes the character of line break that was not read by the nextInt() previous instead of the value entered:

System.out.println("Ingrese la edad del primer alumno");
int edad1 = sc.nextInt();
// Al presionar ENTER, nextInt() lee el numero ingresado pero no consume el caracter de salto de linea generado al presionar ENTER
e0.ed1(edad1);

System.out.println("Ingrese la carrera");
String carr1 = sc.nextLine();
// nextLine() no va a leer ningun valor ya que consumio el caracter de salto de linea generado anteriormente.
c1.establecercarrera1(carr1);

Possible solutions:

1- Consume the skip character before using the Scanner again:

System.out.println("Ingrese la edad del primer alumno");
int edad1 = sc.nextInt();
e0.ed1(edad1);

sc.nextLine();  // Consume el caracter de salto de linea

System.out.println("Ingrese la carrera");
String carr1 = sc.nextLine();
// nextLine() lee el valor ingresado y a diferencia de nextInt(), si consume el caracter de salto de linea
c1.establecercarrera1(carr1);

2- Use nextLine() to read the data entered, which will also consume the character of line break. Since nextLine() returns a String , you have to convert it to the desired data type, in this case int :

System.out.println("Ingrese la edad del primer alumno");
int edad1 = Integer.parseInt(scanner.nextLine()); // Lee el valor ingresado y tambien consume el caracter de salto de linea, luego lo convierte a int.
e0.ed1(edad1);

System.out.println("Ingrese la carrera");
String carr1 = sc.nextLine();
c1.establecercarrera1(carr1);

For this solution it is advisable to do the conversion within a block try catch to control possible errors in the conversion.

    
answered by 21.09.2018 в 18:51