1

My program should ask users to enter some value and evetually those value should be printed out depending on the menu option choosen (in my case it's 3). I have used System.out.println("Your name: " + name); to print out the name inserted by user, but unfortunately it can't print out the name. The line is just left empty. Why so? How can I fix it.

    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/withdraw\n"
            + "3. View details\n"
            + "4. Deleting an account\n"//not used yet
            + "5. View all the accounts\n"//not used yet
            + "6. Quit\n\n");
    System.out.println("*************\n"
            + "************");
    while (true) {
        userChoose = sc.nextInt();

        if (userChoose == 1) {

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

            name = sc.nextLine();
            sc.nextLine();

            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) {

               initiateAmount = initiateAmount + depOrWith;
            } else {

               initiateAmount = 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: " + initiateAmount);
            System.out.println("\n-----------\n"
                    + "------------");
        } else if (userChoose == 6) {

            System.exit(0);

        }

    }

}
Riyana
  • 215
  • 7
  • 27
  • 1
    Did you execute the case 1 first? Because that is where the input is read and stored in `name` – thefourtheye Apr 05 '15 at 10:04
  • Please learn to format your code correctly; this is wholly illegible. – Boris the Spider Apr 05 '15 at 10:04
  • name is null in option 3, also use switch to make the conditions simpler. – Jack Apr 05 '15 at 10:06
  • Boris the Spider, its easy to comment like this, but it's not easy to provide a good answer. By the way, can u tell me the wrong with my code formatting? which parth looks illigible and how to fix it? – Riyana Apr 05 '15 at 10:28

4 Answers4

2

After selecting the option and pressing Enter, the Scanner is not reading the newline. Afterwards when name = sc.nextLine(); is called it will only read the new line following the selected option and name will be assigned the empty string. To solve this, simply add a call nextLine after reading the selected option, and remove the duplicate nextLine when reading the name:

while (true) {
    userChoose = sc.nextInt();
    sc.nextLine();

    if (userChoose == 1) {

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

        name = sc.nextLine();

        System.out.println("Choose an account number:");

        ...
M A
  • 65,721
  • 13
  • 123
  • 159
1

Interchange the Blank nextLine() and the assigning one.

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

        sc.nextLine();
        name = sc.nextLine();

        System.out.println("Choose an account number:");

        accNum = sc.nextInt();
0

name is null. If the user selects three you haven't actually assigned a value to the name variable yet, that only happens if they choose 1

MrB
  • 757
  • 6
  • 26
0

When you select the option 3 you're not setting the name value which defaults to null, so it's printing Your name: null

You need to read and assign name value if you want something to be printed.

Neumann
  • 311
  • 3
  • 15