扫描仪问题,输入信息无效

时间:2013-02-26 10:23:06

标签: java input output

问题,我看了几百万次的代码,可能是我看不到简单的答案,但为什么在作者姓名和作者姓氏这一点,系统打印出来像这样:

Enter author Name: Enter author Surname: 

它应该是这样的......

Enter author Name: 
Enter author Surname:

如果我在系统中输入信息,请记住只有作者名字,我不能为姓氏输入信息。

import java.util.Scanner;

public class BookTest {

    public static void main(String[] args) {    

        Scanner scan = new Scanner (System.in);

        Book bookIn = new Book();

        System.out.print("Enter book title: ");
        bookIn.setTitleBook(scan.nextLine());



        System.out.print("Enter author Name: ");
        bookIn.setFirstName(scan.nextLine());

        System.out.print("Enter author Surname: ");
        bookIn.setSecondName(scan.nextLine());

        System.out.print("Enter author Nationality: ");
        bookIn.setNationality(scan.nextLine());




        System.out.print("Enter book price: ");
        bookIn.setPriceBook(scan.nextDouble());

        System.out.print("Enter book ISBN: ");
        bookIn.setIsbn(scan.nextInt());




        System.out.println("Title: " +bookIn.getTitleBook());
        System.out.println("Author: "+ bookIn.getFirstName()+ " " + bookIn.getSecondName());
        System.out.println("Price: "+bookIn.getPriceBook());
        System.out.println("ISNB Number: "+bookIn.getIsbn());
        System.out.println("Aurhor Nationality: " + bookIn.getNationality());



    }
}

书类

public class Book {

    private double priceBook;
    private int isbn;
    private String titleBook;
    private String firstName;
    private String secondName;
    private String nationality;


    public void setTitleBook(String titleBook){
        this.titleBook = titleBook;
    }
    public String getTitleBook(){
        return titleBook;
    }


    public void setPriceBook(double priceBook) {
        this.priceBook = priceBook;
    }
    public double getPriceBook() {
        return priceBook;
    }


    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public int getIsbn() {
        return isbn;
    }



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getFirstName() {
        return firstName;
    }




    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }
    public String getSecondName() {
        return secondName;
    }


    public void setNationality(String nationality) {
        this.nationality = nationality;
    }
    public String getNationality() {
        return nationality;
    }



}

1 个答案:

答案 0 :(得分:1)

读取ISBN后,将扫描仪移动到下一行。使用Scanner.nextInt时,输入仅读取数字,而不是按Enter键时创建的新行字符。在Scanner.nextLine读取新行字符后调用Scanner.nextInt并清除缓冲区。

import java.util.Scanner;

public class BookTest {

    public static void main(String[] args) {    

        Scanner scan = new Scanner (System.in);

        Book title = new Book();
        Book price = new Book();
        Book isbn = new Book();
        Book fName = new Book();
        Book sName = new Book();
        Book nation = new Book();

        System.out.print("Enter book title: ");
        title.setTitleBook(scan.nextLine());

        System.out.print("Enter book price: ");
        price.setPriceBook(scan.nextDouble());

        System.out.print("Enter book ISBN: ");
        isbn.setIsbn(scan.nextInt());

        scan.nextLine(); //NOTICE CHANGE HERE
        //System.out.println(); THIS WAS REMOVED


        System.out.print("Enter author Name: ");
        fName.setFirstName(scan.nextLine());

        System.out.print("Enter author Surname: ");
        sName.setSecondName(scan.nextLine());

        System.out.print("Enter author Nationality: ");
        nation.setNationality(scan.nextLine());


    System.out.println("Title: " +title.getTitleBook());
    System.out.println("Price: "+price.getPriceBook());
    System.out.println("ISNB Number: "+isbn.getIsbn());

    System.out.println("Author: "+ fName.getFirstName() + sName.getSecondName());
    System.out.println("Aurhor Nationality: " + nation.getNationality());



    }
}
相关问题