How can I read input from the console using the Scanner class in Java?

前端 未结 15 1831
我寻月下人不归
我寻月下人不归 2020-11-21 06:19

How could I read input from the console using the Scanner class? Something like this:

System.out.println(\"Enter your username: \");
Scanner = i         


        
相关标签:
15条回答
  • 2020-11-21 07:08

    There is problem with the input.nextInt() method - it only reads the int value.

    So when reading the next line using input.nextLine() you receive "\n", i.e. the Enter key. So to skip this you have to add the input.nextLine().

    Try it like that:

     System.out.print("Insert a number: ");
     int number = input.nextInt();
     input.nextLine(); // This line you have to add (it consumes the \n character)
     System.out.print("Text1: ");
     String text1 = input.nextLine();
     System.out.print("Text2: ");
     String text2 = input.nextLine();
    
    0 讨论(0)
  • 2020-11-21 07:09

    A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in. It's really quite simple.

    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();
    

    To retrieve a username I would probably use sc.nextLine().

    System.out.println("Enter your username: ");
    Scanner scanner = new Scanner(System.in);
    String username = scanner.nextLine();
    System.out.println("Your username is " + username);
    

    You could also use next(String pattern) if you want more control over the input, or just validate the username variable.

    You'll find more information on their implementation in the API Documentation for java.util.Scanner

    0 讨论(0)
  • 2020-11-21 07:10
    import java.util.Scanner;
    
    public class ScannerDemo {
        public static void main(String[] arguments){
            Scanner input = new Scanner(System.in);
    
            String username;
            double age;
            String gender;
            String marital_status;
            int telephone_number;
    
            // Allows a person to enter his/her name   
            Scanner one = new Scanner(System.in);
            System.out.println("Enter Name:" );  
            username = one.next();
            System.out.println("Name accepted " + username);
    
            // Allows a person to enter his/her age   
            Scanner two = new Scanner(System.in);
            System.out.println("Enter Age:" );  
            age = two.nextDouble();
            System.out.println("Age accepted " + age);
    
            // Allows a person to enter his/her gender  
            Scanner three = new Scanner(System.in);
            System.out.println("Enter Gender:" );  
            gender = three.next();
            System.out.println("Gender accepted " + gender);
    
            // Allows a person to enter his/her marital status
            Scanner four = new Scanner(System.in);
            System.out.println("Enter Marital status:" );  
            marital_status = four.next();
            System.out.println("Marital status accepted " + marital_status);
    
            // Allows a person to enter his/her telephone number
            Scanner five = new Scanner(System.in);
            System.out.println("Enter Telephone number:" );  
            telephone_number = five.nextInt();
            System.out.println("Telephone number accepted " + telephone_number);
        }
    }
    
    0 讨论(0)
提交回复
热议问题