英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论