I continue with my exercises in java and I find that when I want to reuse the ISBN variable to start setting the data for book two, when I execute, that line does not give me the scanner an option and asks me for the following variable.
/**
* ProbarLibro.java
* Programa para probar la clase Libro
* ==================================
*
* @author Luis Quesada Romero
*/
// Pertece al paquete tarea_libro
package tarea_libro;
import java.util.Scanner;
public class ProbarLibro { // Clase principal ProbarLibro
public static void main(String[] args) { // Método Main
// Varialbes
String comenzamos;
String respuestaSi = "s";
String ISBN;
String titulo;
String autor;
short paginas;
// Inicio del programa
System.out.println("BIENVENIDO\n");
System.out.println("Este programa guarda los datos de dos libros y nos dira cual tiene más paginas");
System.out.println("¿Comenzamos? s/n");
// Guardamos la respuesta y la evaluamos para empezar o salir del programa
Scanner entrada = new Scanner(System.in);
comenzamos = entrada.nextLine();
if ( respuestaSi.equals(comenzamos) ) {
// Se piden datos del primer libro y se instancia
Libro libroUno = new Libro();
// ISBN 1
System.out.print("\nISBN del primer Libro: ");
ISBN = entrada.nextLine();
libroUno.setISBN(ISBN);
// Titulo 1
System.out.print("Título del primer Libro: ");
titulo = entrada.nextLine();
libroUno.setTitulo(titulo);
// Autor 1
System.out.print("Autor del primero Libro: ");
autor = entrada.nextLine();
libroUno.setAutor(autor);
// Páginas 1
System.out.print("Páginas del primer Libro: ");
paginas = entrada.nextShort();
libroUno.setPaginas(paginas);
// Se piden los datos del segundo libro y se instancia
Libro libroDos = new Libro();
// ISBN 2
System.out.print("\nISBN del segundo Libro: ");
ISBN = entrada.nextLine();
libroDos.setISBN(ISBN);
// Título 2
System.out.print("Título del segundo libro: ");
titulo = entrada.nextLine();
libroDos.setTitulo(titulo);
// Autor 2
System.out.print("Autor del segundo Libro: ");
autor = entrada.nextLine();
libroDos.setAutor(autor);
// Páginas 2
System.out.print("Págionas del segundo Libro: ");
paginas = entrada.nextShort();
libroDos.setPaginas(paginas);
} else {
System.out.println("Otra vez sera, buena suerte!");
}
} // Fin del método main
} // Fin de la clase pirncipal ProbarLibro
I also leave you the book class in case the problem is from this file
/**
* Libro.java
* Definición de la clase Libro
* ============================
*
*@author Luis Quesada Romero
*/
// Pertenece al paquete tarea_libro
package tarea_libro;
public class Libro { // Clase Libro
// ATRIBUTOS //////////////////////////////////////////////////////////
String ISBN;
String titulo;
String autor;
short paginas;
// MÉTODS /////////////////////////////////////////////////////////////
/**
* Constuctores principal
* ===================
*
* @param ISBN
* @param titulo
* @param autor
* @param paginas
*/
public Libro (String ISBN, String titulo, String autor, short paginas){
this.ISBN = ISBN;
this.titulo = titulo;
this.autor = autor;
this.paginas = paginas;
}
public Libro() {
}
/*
* Metodos Getter
*/
public String getISBN() {
return ISBN;
}
public String getTitulo() {
return titulo;
}
public String getAutor() {
return autor;
}
public short getPaginas() {
return paginas;
}
/*
* Métodos Setter
*/
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public void setAutor(String autor) {
this.autor = autor;
}
public void setPaginas(short paginas) {
this.paginas = paginas;
}
} // Fin de la clase Libro
As I told you, I set book one well, but when I start with the data from book two, the first data I ask for from book two does not give me the option to enter it, it jumps directly to the second data.
It is probably because you are reading a short before and it does not consume the line break, it only consumes the number. Example, if you put 80 in pages, it is reading "80" but it stays "\n" that stays in the scanner and that is why you do not it asks for the ISBN because it interprets the "\n" that was left as if that were your answer. To solve it, just put a next line before reading your ISBN
The nextShort() method advances on the same line, so in order to advance to the next line after using it you must use the nextLine() method.
Example:
Update
You have a veil as if Scanner was reading a file line by line. nextLine steps through the lines and nextShort() and nextInt() through the numeric values of each line.
Ex:
In the first line to advance between the numbers it has, you have to use nextInt(). And to advance to the next line, where "Hello World" is, you have to use nextLine().
nextInt()
gets the numeric value and advances to the next value.nextLine()
Gets the value of the line and advances to the next line.