为了账号安全,请及时绑定邮箱和手机立即绑定

不使用 NextLine 读取字符串中的空格作为输入

不使用 NextLine 读取字符串中的空格作为输入

跃然一笑 2021-09-03 15:37:10
说明:我在下面的代码中使用了几个扫描仪语句。我首先阅读“地址”,然后阅读“手机号码”。然后是来自用户的一些随机内容。当我使用 adress=sc.next()它从用户(无空格)读取地址的字符串值并转到下一个扫描语句,即mobile=sc.nextBigInteger()。通过使用此方法,我无法读取“地址(字符串)”中的空格,并且将运行时错误作为 inputMismatchException 抛出。现在,如果我使用adress=sc.NextLine,程序会直接跳转到mobile=sc.nextBigInteger().在上述情况和以下代码中,如何将空格读取为输入。我如何保护自己免受运行时错误的影响。我在论坛上遇到过类似的问题,但没有一个是令人满意的。谢谢你。(问我是否需要有关问题的更多信息)预期: 输入(在地址字符串中):印度马哈拉施特拉邦浦那输出(在显示功能中):印度浦那马赫拉施特拉邦。究竟发生了什么: 如果输入(在地址字符串中):浦那输出(在显示功能中):浦那如果输入(在地址字符串中):印度浦那(现在只要我输入字符串并在那里点击输入,我就会收到运行时错误作为输入不匹配异常)> JAVA    public class Data {           Scanner sc = new Scanner(System.in);          String adress;          BigInteger AcNo;          BigInteger mobile;          String ifsc;        void getData() {                                                                           System.out.println("Welcome to  Bank System");          System.out.println("Please Enter the adress :");           adress= sc.next();           System.out.println("Enter the Mobile Number");             mobile = sc.nextBigInteger();           System.out.println("Enter the Account Number");           AcNo = sc.nextBigInteger();           System.out.println("Enter the IFSC Code");           ifsc= sc.next();        }           public static void main(String[] args) {               Data d=new Data();                 d.getData();          }        }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 160 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信