the problem is that when reading data from the keyboard using the methods of the Scanner class, it skips lines (Date and Notes). As far as I understand, the '\ n' or '\ 0' character of the end of the line from the previous input is transmitted.

public void add() { Magazine a = new journal(); System.out.println("Add journal"); Scanner sc = new Scanner(System.in); System.out.println("Titre of journal"); String titre= sc.nextLine(); a.setTitre(titre); System.out.println("Price journal"); float prix=sc.nextFloat(); a.setPrix(prix); //袙袨孝 协孝校 System.out.println("Date of Magazine"); date= sc.nextLine(); a.setDate(date); System.out.println("Category of journal"); int category = sc.nextInt(); a.setCategory(category); //袙袨孝 协孝校 System.out.println("Notes of journal"); String notes = sc.nextLine(); a.setNotes(notes); Bibliotheque.GetList().add(a); } 
  • If the answer helped you, could you accept it? - Axenow

1 answer 1

The thing is that the commands next() , nextFloat() , etc. - they only read the token, that is, a sequence of characters that are not control. Therefore, when you write nextFloat() and then nextLine() , the Scanner first reads Float , and then in the next query it will go to the end of the line, which comes right here at once.

This is how it will work:

 public void add() { System.out.println("Add journal"); Scanner sc = new Scanner(System.in); System.out.println("Titre of journal"); String titre= sc.nextLine(); System.out.println("Price journal"); float prix=sc.nextFloat(); sc.nextLine(); //袙袨孝 协孝校 System.out.println("Date of Magazine"); String date= sc.nextLine(); System.out.println("Category of journal"); int category = sc.nextInt(); sc.nextLine(); //袙袨孝 协孝校 System.out.println("Notes of journal"); String notes = sc.nextLine(); System.out.println(titre); System.out.println(prix); System.out.println(date); System.out.println(category); System.out.println(notes); } 

Result:

 Add journal Titre of journal >> sdflkj sdkfsd sdf Price journal >> 123.1 Date of Magazine >> 12.12.12 Category of journal >> 2 Notes of journal >> dsfs fsdf sdf jdskfjksdf sdflkj sdkfsd sdf 123.1 12.12.12 2 dsfs fsdf sdf jdskfjksdf 

To understand how this works best, I would advise you to look at the source code for the nextInt () function. It's pretty simple, but it will show how it works.