从 System.in 获取输入时,在线编译器导致崩溃

时间:2021-04-29 08:54:21

标签: java

使用在线编译器时收到如下错误代码(可以在这里测试在线代码-> http://tpcg.io/1Q93ObOJ)

$java -Xmx128M -Xms16M PassVerification
Please enter a new password.
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at PassVerification.main(PassVerification.java:11)

enter image description here

我的初学者知识收到的错误是命令 nextLine();使用它会自动跳过用户输入。我的问题是如何修复它以便它可以读取用户输入并能够运行必要的 if 和 else 语句以确保正在创建的密码被认为是强密码。

import java.util.Scanner;

public class PassVerification {


public static void main(String[] args){
    Scanner run = new Scanner(System.in);
    
    System.out.println("Please enter a new password.");
    
    String pass = run.nextLine(); //crash occurs here.
    
    System.out.println("User input = "+pass);
 
    //trim codes for validating password, but unrelated to crash...
      
}
}   

1 个答案:

答案 0 :(得分:2)

看起来您使用的在线 Java 编译器 https://www.tutorialspoint.com/compile_java_online.php 具有用于标准输入的单独选项卡。在 stdin 选项卡中添加输入字符串后,执行完成。或者,您可以尝试下面的在线编译器,它具有易于阅读的 UI。 https://onecompiler.com/java/3wvzssxvd