1

My program will ask the user to enter his/her name. At the moment my scanner object can read only a single word. But if the user enters muliple words (let's say someone's full name), then it throws exception. Like this:

Exception

How can I solve this?

My code:

    public class BankApp_Assignment {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int userChoose;
        String name = null;
        int accNum = 0;
        double InitiateAmount = 0;
        double newAmm = 0;

        double depOrWith = 0;
        System.out.println("WELCOME TO OUR BANK!\n\n"
                + "...................\n"
                + "...................\n\n"
                + "Choose your optiin:\n"
                + "1. Create new account\n"
                + "2. Deposit\n"
                + "3. View details\n"
                + "4. Quit\n\n");
        while (true) {
            userChoose = sc.nextInt();

            if (userChoose == 1) {

                System.out.println("Enter your full name:");

                name = sc.next();
                System.out.println("Choose an account number:");

                accNum = sc.nextInt();
                System.out.println("Enter an initiating amount:");

                InitiateAmount = sc.nextDouble();
                System.out.println("\n-----------\n"
                        + "------------");
            } else if (userChoose == 2) {

                System.out.println("Enter negative value to withdraw and positive to deposit");
                depOrWith = sc.nextInt();
                if (depOrWith < 0) {

                    newAmm = InitiateAmount + depOrWith;
                } else {

                    newAmm = InitiateAmount + depOrWith;
                }

                System.out.println("\n-----------\n"
                        + "------------");

            } else if (userChoose == 3) {

                System.out.println("Your name: " + name);
                System.out.println("Your account number: " + accNum);
                System.out.println("Your current balance: " + newAmm);
                System.out.println("\n-----------\n"
                        + "------------");
            } else if (userChoose == 4) {

                System.exit(0);

            }

        }

    }

}
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
Riyana
  • 215
  • 7
  • 27

1 Answers1

2

You need to change

name = sc.next();

sc.next() will read only till the space nothing after that

name = sc.nextLine();

sc.nextLine() will read the whole line.

Note you need to be careful of skipping read Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods.

Community
  • 1
  • 1
StackFlowed
  • 6,575
  • 1
  • 26
  • 41
  • StackFlowed, thanks for quick answer. but using nextLine() skips one read line. I tried hard to fixed it. I have looked at your suggestion links. but it doesn't helps that much. What I did to solve this issue is just declaring the variable name at the same line with nextLine() method call, but then it shows an error in my third else-if statemnt (where userChoose==3). it cant find the variable name "name". please tell me how can solve it? – Riyana Apr 03 '15 at 14:37
  • StackFlowed, ignore my previous comment. problem is solved now. YAHOO i made it! – Riyana Apr 03 '15 at 14:43