He asks me for two values at the same time [duplicated]

0

I have a problem, I can not enter a value for a variable keyboard because I get 2 messages at the same time

package libro;

import java.util.*;

public class Main {

    public static void main(String[] args) {

        //libro1
        Scanner sc = new Scanner(System.in);

        System.out.println("Digite el autor del libro");

        String autor1 = sc.nextLine();

        System.out.println("Digite el numero de ISNB");

        int ISNB1 = sc.nextInt();

        System.out.println("Digite el titulo del libro");

        String titulo1 = sc.nextLine();

        System.out.println("Digite el número de páginas");

        int numPaginas1 = sc.nextInt();

        Libro libro1 = new Libro(ISNB1, titulo1, autor1, numPaginas1);

        System.out.println("LIBRO1");

        System.out.println(libro1); //estado del objeto con metodo toString

    }

}

    
asked by Andrew Xymind 29.04.2018 в 22:12
source

1 answer

0

Good one of the solutions is to handle everything with nextLine () and use the java converters to get another type. What happens is that the buffer maintains a \ n after using the nextInt (), this can also be solved using a sc.nextLine () (This gets in \ n what causes that jump) after obtaining a numeric data.

package libro;

import java.util.*;

public class Main {

public static void main(String[] args) {

    //libro1
    Scanner sc = new Scanner(System.in);

    System.out.println("Digite el autor del libro");

    String autor1 = sc.nextLine();

    System.out.println("Digite el numero de ISNB");

    int ISNB1 = Integer.parseInt(sc.nextLine());

    System.out.println("Digite el titulo del libro");

    String titulo1 = sc.nextLine();

    System.out.println("Digite el número de páginas");

    int numPaginas1 = Integer.parseInt(sc.nextLine());

    Libro libro1 = new Libro(ISNB1, titulo1, autor1, numPaginas1);

    System.out.println("LIBRO1");

    System.out.println(libro1); //estado del objeto con metodo toString

   }

}

--------------------------------- Version 2 ------------ ------

System.out.println("Digite el numero de ISNB");
int ISNB1 = sc.nextInt();
sc.nextLine();
System.out.println("Digite el titulo del libro");

String titulo1 = sc.nextLine();

System.out.println("Digite el número de páginas");
int numPaginas1 = sc.nextInt();
    
answered by 29.04.2018 в 22:33