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

前端 未结 15 1942
我寻月下人不归
我寻月下人不归 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: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  ");
          System.exit(1);
      }
      try {
          grep(args[0], args[1]);
      } catch (IOException e) {
          System.out.println(e.getMessage());
      }
      

提交回复
热议问题