简体   繁体   中英

Java simple program not allowing to take user input

I'm trying to make a simple java program and basically I am taking some user input values and printing it out in a neatly organized output. This is all in the console. But when I continue to add more inputs, the program just doesn't let me take the input. Here's my code:

import java.util.*;

public class output {
    public static void main(String arg[]) {

        //================================================================================
        // Level 1 Start
        //================================================================================
        Scanner input = new Scanner(System.in);

        //Start asking the user questions and store the values
        System.out.println("Hello! What is your name?");
        String name = input.nextLine();
        System.out.println("Hello " + name + "! Nice to meet you!");

        //================================================================================
        // Level 1 End
        //================================================================================


        //================================================================================
        // Level 2 Start
        //================================================================================
        System.out.println("How old are you?");
        String age = input.nextLine();

        System.out.println("Are you male or female?");
        String gender = input.nextLine();

        System.out.println("How much do you weigh?");
        int weight = input.nextInt();

        System.out.println("Are you a student? (true/false)");
        boolean isAStudent = input.nextBoolean();

        //Add a space
        System.out.println("\n");

        //Display the user's data neatly
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Gender: " + gender);
        System.out.println("Weight: " + weight);

        //check to see if the user is a student and print it out
        if(isAStudent) {
            System.out.println("Student?: Yes");        
        } else {
            System.out.println("Student?: No");
        }

        //================================================================================
        // Level 2 End
        //================================================================================

        //================================================================================
        // Level 3 Start
        //================================================================================

        System.out.println("\n");

        //display hello world a bunch of times
        for(int i = 0; i < 5; i++){
            System.out.println("Hello Hello Hello Hello Hello Hello");
        }

        System.out.println("\n");

        System.out.println("Tell me a quote");
        String quote = input.nextLine();

        System.out.print(quote);    

        //================================================================================
        // Level 3 End
        //================================================================================

    }
}

I know I'm not supposed to put huge chunks of code on stackoverflow, but I feel like the other parts could lead to the solution. So my problem is, When I get to level 3 (check comments) and to the print statement where it says "Tell me a quote", I'm not able to take the input for the line after. So the input for the String quote acts as if I had hit the enter key even if I didn't. So, it's taking the value before I'm even able to type anything. Please help me... If you need more explaining, let me know.

After this,

boolean isAStudent = input.nextBoolean();

a newline character remains in the buffer. You should get rid of it:

boolean isAStudent = input.nextBoolean();
input.nextLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM