Search code examples
javaexceptionjava.util.scannerspacesinputmismatchexception

Read spaces in String as Input Without using NextLine


Discription:

I used several scanner statments in below code. First i Read the "Adress" and then "Mobile No." and then some random stuff from user.

When i use adress=sc.next()

it reads the string value of adress from user(Without space) and go to the Next scan statement i.e. mobile=sc.nextBigInteger(). by using this method i am not able read the spaces in "adress(string) " and it throws the runtime error as inputMismatchException.

Now if i use the adress=sc.NextLine, the Progrram directly jumps to the mobile=sc.nextBigInteger().

How is it possible to read the spaces as input in above situations and the below code. How can i protect myself from runtime Errors. I got similar questions on the forum but non of them was satisfactory. Thank you. (Ask me if you need more information about question)

Expected: input(In adress string) : pune maharashtra india

output(in display function) : pune mahrashtra india.

What exactly happened: if input(in adress string) : pune output(in display function) : pune

if input(in adress string) : Pune india
(Now As soon as i entered the string and hit the enter there I get a runtime error as an inputmismatchexception )

> 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();
          }
        }

Solution

  • Change this line ;

    adress= sc.next();
    

    with this line ;

    adress = sc.nextLine();
    

    this will solve your problem. scan.nextLine() returns everything up until the next new line delimiter, or \n ,but scan.next() is not.

    So all code will be like this ;

    public class Data{
    
        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 = new Scanner(System.in).nextLine();
    
            System.out.println("Enter the Mobile Number");
            mobile = new Scanner(System.in).nextBigInteger();
    
            System.out.println("Enter the Account Number");
            AcNo = new Scanner(System.in).nextBigInteger();
    
            System.out.println("Enter the IFSC Code");
            ifsc = new Scanner(System.in).next();
        }
    
        public static void main(String[] args) {
            Data d = new Data();
            d.getData();
        }
    }