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

说明:


我在下面的代码中使用了几个扫描仪语句。我首先阅读“地址”,然后阅读“手机号码”。然后是来自用户的一些随机内容。


当我使用 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();

          }

        }


跃然一笑
浏览 161回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java