2013-12-20 110 views
0

我有兩個類。我想打印對象的數組。這些對象具有屬性(其中一些屬性爲String s),但是當程序將其讀取爲(nextLine)時,它將不會打印它。爲什麼我的程序不打印nextLine?

這裏是我的意思是:

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"); 
} 

回答

0

嘗試auther=scan.nextLine();前添加sc.nextLine();。它可能需要刷新。

0

scan.nextInt()命令只讀取int值,並開始編碼的時候,讓你必須添加scan.nextLine(端線)也喜歡下面

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(); 
+0

這是有趣的我面對java的。它提醒了我過去的日子:) – Vipin

相關問題