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

前端 未结 15 1829
我寻月下人不归
我寻月下人不归 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 06:43
    Scanner scan = new Scanner(System.in);
    String myLine = scan.nextLine();
    
    0 讨论(0)
  • 2020-11-21 06:43

    You can use the Scanner class in Java

    Scanner scan = new Scanner(System.in);
    String s = scan.nextLine();
    System.out.println("String: " + s);
    
    0 讨论(0)
  • 2020-11-21 06:50

    A simple example:

    import java.util.Scanner;
    
    public class Example
    {
        public static void main(String[] args)
        {
            int number1, number2, sum;
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter First multiple");
            number1 = input.nextInt();
    
            System.out.println("Enter second multiple");
            number2 = input.nextInt();
    
            sum = number1 * number2;
    
            System.out.printf("The product of both number is %d", sum);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:50

    There is a simple way to read from the console.

    Please find the below code:

    import java.util.Scanner;
    
        public class ScannerDemo {
    
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
    
                // Reading of Integer
                int number = sc.nextInt();
    
                // Reading of String
                String str = sc.next();
            }
        }
    

    For a detailed understanding, please refer to the below documents.

    Doc

    Now let's talk about the detailed understanding of the Scanner class working:

    public Scanner(InputStream source) {
        this(new InputStreamReader(source), WHITESPACE_PATTERN);
    }
    

    This is the constructor for creating the Scanner instance.

    Here we are passing the InputStream reference which is nothing but a System.In. Here it opens the InputStream Pipe for console input.

    public InputStreamReader(InputStream in) {
        super(in);
        try {
            sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## Check lock object
        }
        catch (UnsupportedEncodingException e) {
            // The default encoding should always be available
            throw new Error(e);
        }
    }
    

    By passing the System.in this code will opens the socket for reading from console.

    0 讨论(0)
  • 2020-11-21 06:51

    When the user enters his/her username, check for valid entry also.

    java.util.Scanner input = new java.util.Scanner(System.in);
    String userName;
    final int validLength = 6; // This is the valid length of an user name
    
    System.out.print("Please enter the username: ");
    userName = input.nextLine();
    
    while(userName.length() < validLength) {
    
        // If the user enters less than validLength characters
        // ask for entering again
        System.out.println(
            "\nUsername needs to be " + validLength + " character long");
    
        System.out.print("\nPlease enter the username again: ");
        userName = input.nextLine();
    }
    
    System.out.println("Username is: " + userName);
    
    0 讨论(0)
  • 2020-11-21 06:54
    1. To read input:

      Scanner scanner = new Scanner(System.in);
      String input = scanner.nextLine();
      
    2. To read input when you call a method with some arguments/parameters:

      if (args.length != 2) {
          System.err.println("Utilizare: java Grep <fisier> <cuvant>");
          System.exit(1);
      }
      try {
          grep(args[0], args[1]);
      } catch (IOException e) {
          System.out.println(e.getMessage());
      }
      
    0 讨论(0)
提交回复
热议问题