2

I know I have to use the Scanner class to first create the scanner object to receive user input in java, although I don't know how to input multiple words into one string variable. For example, I have this code here:

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

Which according to many forums, should allow the user to input the entire line into the String variable, but instead, it skips the Author prompt entirely. What am I doing wrong?

EDIT: I have added the full loop below:

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

3 Answers 3

4

Ordinarily this does work but more than likely you are using this code in a loop. Since nextDouble does not consume newline characters you will need to consume this character explicitly

pages = input.nextDouble();
input.readLine() // added - consumes newline
Sign up to request clarification or add additional context in comments.

Comments

2

Since you are running it inside a loop

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

This will solve it. That's why Author prompt just skips to next since it has got the input. i.e \n (new line character from previous double input value)

3 Comments

Am I missing something, where has he said this is in a loop?
Generally, skipping problem happens in loop because of unconsumed characters
I tried adding input.readLine() but netbeans gave me errors. From what I understand, netbeans is taken each word for the author name as separate variables.
1

Do you have this in a loop? There's probably a /n character still in the buffer. At the top, add: input.nextLine();

1 Comment

Yes, sorry this is in a switch statement that's within a do-while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.