0

I have two classes. I want to print arrays of objects. These objects have attributes (some of them are Strings), but when the program reads it as (nextLine), it will not print it.

Here is what I mean:

public static void main(String[] args){
String auther,Title,genre;
int ISBN,publicationYear,ISBN2;


if (Book.getBookNo()< archive_size){
    System.out.println("ISBN: ");
    ISBN=scan.nextInt();
    System.out.println("Auther: ");
    auther=scan.nextLine(); //here is the problem if I choose next it's ok but no nextLine
    scan.nextLine();
    System.out.println("Published Year");
    publicationYear=scan.nextInt();
    System.out.println("Title: ");
    Title=scan.nextLine();
    scan.nextLine();
    System.out.println("Genre: ");
    genre=scan.next();
    libraryBooks[Book.getBookNo()] = new Book(ISBN,auther,publicationYear,Title,genre);

}

public static void printAll(){
    int i=0;
    for ( i=0 ; i<Book.getBookNo(); i++)
        System.out.println("Book "+(i+1)+"\nISBN: "+libraryBooks[i].getISBN()+
        "\nAuthor:           "+libraryBooks[i].getAuther()+
        "\npublishedyear: "+libraryBooks[i].getPublication()+"\nTitle: "+libraryBooks[i].getTitle()+
        "\nGenre: "+libraryBooks[i].getGenre()+"\n");
}
David Makogon
  • 64,809
  • 21
  • 130
  • 175

2 Answers2

0

Try adding sc.nextLine(); before auther=scan.nextLine();. It may need a flush.

Sergi
  • 583
  • 3
  • 14
0

scan.nextInt() command only reads the int value , and to get the end line you have to add scan.nextLine() also like below

System.out.println("ISBN: ");
ISBN=scan.nextInt();
scan.nextLine(); // It will solve the problem in question
System.out.println("Auther: ");
auther=scan.nextLine(); //here is the problem if I choose next it's ok but no nextLine
scan.nextLine();
System.out.println("Published Year");
publicationYear=scan.nextInt();
Vipin
  • 4,013
  • 3
  • 28
  • 52