1

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();
          }
        }
Aniket Jadhav
  • 508
  • 4
  • 16
  • Please show valid input and desired output. – merlin2011 Sep 06 '18 at 20:42
  • when you ask the user to `"Enter the Mobile Number"`, you are inputting `maharashtra`, which is a string. Meanwhile, your `sc` is expecting a `BigInteger`, hence the `InputMisMatch` – user3170251 Sep 06 '18 at 20:58
  • but i inputting the maharshtra in adress field and when i hit enter then it shows an inputMismatchDxception. – Aniket Jadhav Sep 06 '18 at 21:03
  • @merlin2011 I shown what you said – Aniket Jadhav Sep 06 '18 at 21:05
  • you Should re-design your code and use regular expression for working with the input. Try writing a method which asks for a single property of the class and stores the user input. Then cycle that for every property of your class and for each iteration check with the appropriate regex(cfr the telephone number). Also treat everything as a string for simplicity – majik Sep 06 '18 at 21:16
  • Why can't you use `nextLine()`? – AsherMaximum Sep 06 '18 at 21:40
  • @AsherMaximum I clearly mentioned above what I want.Thank You – Aniket Jadhav Sep 06 '18 at 21:52
  • @majik I don't understand what exactly you want to say. please try to run the code if anything wrong. – Aniket Jadhav Sep 06 '18 at 22:37

1 Answers1

0

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();
    }
}
drowny
  • 2,067
  • 11
  • 19
  • They're specifically asking for how to do it without using `nextLine`. Why, I do not know, as that is the right solution. – AsherMaximum Sep 06 '18 at 21:41
  • It doesnot make sense. If he wants to get all of line , he should get with this command. It's so clear and easy i think. – drowny Sep 06 '18 at 21:45
  • I tried and it works correctly. Asking address field and set to field. – drowny Sep 06 '18 at 21:48
  • @drowny Now if I changed as what you said, The code Jumps to next statement to "mobile No," by skipping the adress field. I mentioned same in the above description. Thank you – Aniket Jadhav Sep 06 '18 at 21:59
  • Can you try now please @AniketJadhav ? I changed with get attributes with new scanners. – drowny Sep 06 '18 at 22:08
  • @drowny Thank you....... This is the answer I was looking for. it Worked fine for me as I want. Thank you again...... if you don't mind can you explain shortly about what you exactly did. – Aniket Jadhav Sep 06 '18 at 22:23
  • Of course ; According to [ScannerDoc](https://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html) , there is a sentence which is `When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.` . So your scanner if throws exception, this will work after other scanners method call , if not method will end. So stores this exception in your class, and throws in other method call. I hope you can understand very well. – drowny Sep 06 '18 at 22:38