英文:
How do i give null as an input through scanner in java
问题
Scanner s = new Scanner(System.in);
String a = s.next();
System.out.println("this is: " + a);
我尝试输入一个 NULL。但是如果我在输出控制台中输入 null,它会将其视为普通字符串。
英文:
Scanner s= new Scanner(System.in);
String a=s.next();
System.out.println("this is: "+a);
I tried taking a NULL as input.But if i give null in the output console,it is treating it as a normal String.
答案1
得分: 1
Scanner.next 返回一个 String。如果您想将输入解析为其他类型,可以使用 Scanner.next* 方法,例如 Scanner.nextFloat 等。
如果您需要将字符串“null”视为 null 进行处理,您必须自行解析。
英文:
Scanner.next returns a String. If you want to parse the input into some other type, you can use Scanner.next* methods, e.g. Scanner.nextFloat, etc.
If you need to handle "null" as a string to mean null you have to parse it yourself.
专注分享java语言的经验与见解,让所有开发者获益!

评论