如何在Java中通过Scanner输入null?

huangapple go评论85阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月27日 19:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定