如何解决如何在Java中读取空行?
如果用户按下Enter键,我希望能够读取一个空行,但是同时,如果他插入任何内容,我希望能够读取包含内容的行。
System.out.println("Name> ");
String nome = sc.nextLine();
System.out.println("--" + nome + "--");
System.out.println("Id> ");
String id = sc.nextLine();
System.out.println(" ")
问题在于,每次用户插入任何内容时,我只会收到一个空字符串。
解决方法
我认为sc是扫描程序对象,所以我已经完成了这样的代码。
SELECT ROW_NUMBER() OVER (ORDER BY userid) AS userid,username,user_phone_no AS mobile_no
FROM
(
SELECT p.*,ROW_NUMBER() OVER (PARTITION BY username ORDER BY userid) AS rn
FROM phone_numbers p
WHERE phone_number_type ='mobile'
) p
WHERE rn = 1
ORDER BY userid
这很好
Scanner sc = new Scanner(new InputStreamReader(System.in));
System.out.println("Name> ");
String nome = sc.nextLine();
System.out.println("--" + nome + "--");
System.out.println("Id> ");
String id = sc.nextLine();
System.out.println(" ");
和
this is the output for a line with content
Name>
wahidullah
--wahidullah--
Id>
200
希望这对您有用
,我同意@ Wahidullah Shah。
但是,我认为您遇到的问题是由 nextInt()之后紧接使用 nextLine() 引起的em>, next() 等。 我基于您只提供了一小段代码的事实做出了这种假设。
问题在于, nextInt() 不使用 返回字符 。此后立即调用 nextLine() 时,它仅消耗 返回字符 ,从而为您提供空字符串
研究链接:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。