java - 读取字符串时遇到问题
问题描述
Scanner s = new Scanner(System.in)
int x = S.nextInt();
double y = S.nextDouble();
S.nextLine(); //Why
String z = S.nextLine();
为什么我需要S.nextLine()
在读取 int & double 之后才能读取 String ?
解决方案
如果您有以下信息
Line 1: 1.0 2.0 4.0(you are currently here after reading the double 4.0) \n
Line 2: (you are here once you eat that '\n' character using readLine())
一旦你存储了最后一个数字:4,你仍然在同一行(第 1 行)。你需要吃掉那个换行符。s.nextLine()
将读取到行尾并返回空结果。完成后,扫描仪将位于第 2 行的开头。
推荐阅读
- android - 如何添加应用程序防护以防止对 Flutter 应用程序进行逆向工程(就像在 Android 中所做的那样)?
- python - pandas 根据多个条件替换 np.nan
- python - 如何在数据框中按组对行进行排序
- karate - 当我尝试使用空手道机器人自动化记事本时,荧光笔未检测到
- powershell - 通过 ADO 管道执行 PowerShell Selenium 脚本时面临代理问题:远程服务器返回错误:(403)禁止
- amazon-web-services - Terraform 输入变量的最佳解决方案?
- reactjs - 在特定页面中反应隐藏菜单和页脚
- ios - 通过 FirebaseMessaging 背景消息显示 iOS 应用徽章
- c# - 使用 WebApplicationFactory 的模拟身份验证不起作用
- mysql - 如何在包含 SELECT 和 JOIN 语句且仅复制新条目的插入触发器后使用 MySQL?