如何在java中输入字符串?

时间:2014-02-27 14:21:09

标签: java string netbeans

我知道我必须使用Scanner类来首先创建scan对象以接收java中的用户输入,尽管我不知道如何将多个单词输入到一个字符串变量中。例如,我在这里有这个代码:

System.out.println("Please enter the following information");

System.out.print("Author:");
author = input.nextLine();

System.out.print("Title:");
title = input.nextLine();

System.out.print("Grade Level:");
gradeLevel = input.nextDouble();


System.out.print("Pages:");
pages = input.nextDouble();

根据许多论坛,应该允许用户将整行输入到String变量中,而是完全跳过Author提示。我做错了什么?

编辑:我在下面添加了完整的循环:

do {

    System.out.printf("What kind of book is this?\n"
            + "(0 = Textbook, 1 = Novel, 2 = Biography, 3 = Author Information, 4 = Other)");

    switch(input.nextInt()) {
        case 0:
            System.out.println("Please enter the following information.");

            System.out.print("Author:");
            author = input.nextLine();

            System.out.print("Title:");
            title = input.nextLine();

            System.out.print("Grade Level:");
            gradeLevel = input.nextDouble();
            input.readLine();

            System.out.print("Pages:");
            pages = input.nextDouble();

            //Creation of textBooks
            texts[i] = new Textbook(gradeLevel, author, pages, title);

            boolText = true;
            break;
        case 1:
            System.out.print("Please enter the following information.\n");

            System.out.print("Author:");
            author = input.nextLine();
            System.out.print("Genre:");
            genre = input.next();
            System.out.print("Title:");
            title = input.nextLine();
            System.out.print("Pages:");
            pages = input.nextDouble();

            //Creation of Novels
            novels[i] = new Novel(genre, title, author, pages);

            boolNovel = true;
            break;
        case 2:
            System.out.print("Please enter the following information.\n");

            System.out.print("Author:");
            author = input.nextLine();
            System.out.print("Title:");
            title = input.nextLine();
            System.out.print("Subject:");
            subject = input.nextLine();
            System.out.print("Pages:");
            pages = input.nextDouble();

            //Creation of Biographies
            bio[i] = new Biography(subject, title, author, pages);

            boolBiography = true;
            break;
        case 3:
            System.out.print("Please enter the following information.\n");

            System.out.print("Author:");
            author = input.nextLine();
            System.out.print("Email:");
            email = input.next();
            System.out.print("Address:");
            address = input.nextLine();
            System.out.print("Number of Titles:");
            numOfTitles = input.nextDouble();

            //Creation of Author Infortmation
            authors[i] = new Author(author, address, email, numOfTitles);

            boolAuthor = true;
            break;
        case 4:
            System.out.println("Please enter the following information.");

            System.out.print("Title:");
            title = input.nextLine();
            System.out.print("Author:");
            author = input.nextLine();
            System.out.print("Pages:");
            pages = input.nextDouble();

            //Creation of Author Infortmation
            other[i] = new Books(title, author, pages);

            boolOther = true;
        default:
            System.out.println("Program will now terminate.");
            System.exit(1);
    }

    i++;
    }
    while (i < t);

3 个答案:

答案 0 :(得分:4)

通常这确实有用,但很可能你在循环中使用这段代码。由于nextDouble不使用换行符,因此您需要明确使用此字符

pages = input.nextDouble();
input.readLine() // added - consumes newline

答案 1 :(得分:2)

因为你在循环中运行它

pages = input.nextDouble();
input.readLine()

这将解决它。这就是为什么Author提示只是跳到下一个,因为它已经获得了输入。即\n(来自先前双输入值的新行字符)

答案 2 :(得分:1)

你有这个循环吗?缓冲区中可能还有一个/ n字符。在顶部,添加:input.nextLine();