为什么我的程序不打印nextLine?

时间:2013-12-20 13:23:09

标签: java

我有两节课。我想打印对象数组。这些对象具有属性(其中一些是String s),但是当程序将其读取为(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");
}

2 个答案:

答案 0 :(得分:0)

尝试在sc.nextLine();之前添加auther=scan.nextLine();。它可能需要刷新。

答案 1 :(得分: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();