空 Java 扫描仪

L.B*_*L.B 2 java java.util.scanner

我想做一个不能为空的扫描仪。因此,当扫描仪中没有任何内容时按下 Enter 键时,我想发出警告消息。我有这段代码。

  if (user_input.hasNext())// only if scanner is empty and enter is pressed{
        System.out.println("Scanner can not be empty");
    }
Run Code Online (Sandbox Code Playgroud)

rup*_*hye 5

Scanner sc=new Scanner(System.in);
    String input = sc.nextLine();
    if(input.isEmpty())
    {
      System.out.println("Scanner can not be empty");
    }
Run Code Online (Sandbox Code Playgroud)