0

How to get input values properly and store them to use further in this program?

I don't know why but this program is outputting Full Name: and Address: at once and taking input values only for Address:. Also, when I'm outputting New Account option, it is not outputting First Name:.

package com.company;

import java.util.Scanner;

public class Main {

    public static final Scanner input = new Scanner(System.in);
    public String name;
    public int age;
    public long personummer;
    public String address;
    public long phoneNumber;
    public long amount;
    public boolean created = false;

    public static void main(String[] args) {

        Main BankID = new Main();
        int option;

        do {
            System.out.println("- - - - Welcome to Bank of Hkr - - - -");
            System.out.println();
            System.out.println("1) New account");
            System.out.println("2) View account");
            System.out.println("3) Deposit");
            System.out.println("4) Withdraw");
            System.out.println("5) Exit");
            System.out.println();
            System.out.println("- - - - - - - - - - - - - - - - - - - - - ");
            System.out.println();
            System.out.println(">> Please choose an option...");
            option = input.nextInt();

            switch (option) {
                case 1:
                    BankID.newAccount();
                    break;
                case 2:
                    BankID.viewAccount();
                    break;
                case 3:
                    BankID.deposit();
                    break;
                case 4:
                    BankID.withdraw();
                    break;
                default:
                    System.out.println(">> Not A Valid Option ");
            }
        } while (option != 5);
    }

    public void newAccount() {
        System.out.println("New Account Wizard");
        System.out.println("------------------");
        System.out.println("Enter the following details: ");
        System.out.println();
        System.out.println("Full Name: ");
        name = input.nextLine();
        System.out.println("Address: ");
        address = input.nextLine();
        System.out.println("Age: ");
        age = input.nextInt();
        System.out.println("Personummer: ");
        personummer = input.nextLong();
        System.out.print("Phone Number: ");
        phoneNumber = input.nextLong();
        System.out.println("Thanks. Your details have been saved.");
    }

    public void viewAccount() {
        if (created) {
            System.out.println("View Account");
            System.out.println("Full Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Personummer: " + personummer);
            System.out.println("Address: " + address);
            System.out.println("Phone Number: " + phoneNumber);
            System.out.println("Amount: " + amount);
        } else System.out.println("First create an account");
    }

    public void deposit() {
        if (created) {
            System.out.println("Deposit");
            System.out.print("Enter the deposit amount: ");
            amount += input.nextLong();
        } else System.out.println("First create an account");
    }

    public void withdraw() {
        if (created) {
            System.out.println("Withdraw");
            System.out.print("Enter the withdrawal amount: ");
            amount -= input.nextLong();
        } else System.out.println("First create an account");
    }
}

Apologies if the rest of the code isn't related to the question asked. But I'm no expert and might have made some other major mistake in any other part of the code.

Johannes Kuhn
  • 12,878
  • 3
  • 41
  • 66
iamsumitd
  • 1
  • 4

1 Answers1

0

I did not go through all the code but I made some changes to address the issues the mentioned. You will see that I am using a separate Scanner for Strings. Also, I am setting created to true once the account has been created. I also added the case for option 5 for completeness.

import java.util.Scanner;

public class Main {

    public static final Scanner input = new Scanner(System.in);
    public static final Scanner stringInput = new Scanner(System.in);
    public String name;
    public int age;
    public long personummer;
    public String address;
    public long phoneNumber;
    public long amount;
    public boolean created = false;

    public static void main(String[] args) {

        Main BankID = new Main();
        int option;

        do {
            System.out.println("- - - - Welcome to Bank of Hkr - - - -");
            System.out.println();
            System.out.println("1) New account");
            System.out.println("2) View account");
            System.out.println("3) Deposit");
            System.out.println("4) Withdraw");
            System.out.println("5) Exit");
            System.out.println();
            System.out.println("- - - - - - - - - - - - - - - - - - - - - ");
            System.out.println();
            System.out.println(">> Please choose an option...");
            option = input.nextInt();

            switch (option) {
                case 1:
                    BankID.newAccount();
                    break;
                case 2:
                    BankID.viewAccount();
                    break;
                case 3:
                    BankID.deposit();
                    break;
                case 4:
                    BankID.withdraw();
                    break;
                case 5:
                    break;
                default:
                    System.out.println(">> Not A Valid Option ");
            }
        } while (option != 5);
    }

    public void newAccount() {
        System.out.println("New Account Wizard");
        System.out.println("------------------");
        System.out.println("Enter the following details: ");
        System.out.println();
        System.out.println("Full Name: ");
        name = stringInput.nextLine();
        System.out.println("Address: ");
        address = stringInput.nextLine();
        System.out.println("Age: ");
        age = input.nextInt();
        System.out.print("Personummer: ");
        personummer = input.nextLong();
        System.out.print("Phone Number: ");
        phoneNumber = input.nextLong();
        created = true;
        System.out.println("Thanks. Your details have been saved.");
    }

    public void viewAccount() {
        if (created) {
            System.out.println("View Account");
            System.out.println("Full Name: " + name);
            System.out.println("Address: " + address);
            System.out.println("Age: " + age);
            System.out.println("Personummer: " + personummer);
            System.out.println("Phone Number: " + phoneNumber);
            System.out.println("Amount: " + amount);
        } else System.out.println("First create an account");
    }

    public void deposit() {
        if (created) {
            System.out.println("Deposit");
            System.out.print("Enter the deposit amount: ");
            amount += input.nextLong();
        } else System.out.println("First create an account");
    }

    public void withdraw() {
        if (created) {
            System.out.println("Withdraw");
            System.out.print("Enter the withdrawal amount: ");
            amount -= input.nextLong();
        } else System.out.println("First create an account");
    }
}
Matthew Formosa
  • 348
  • 2
  • 8
  • It works. But there is some weird thing about this .next(); It's asking for the *address* after *name* but printing *age* after *name* when asked to print the user info using *view account* option. There's something unclear about this. – iamsumitd Oct 20 '19 at 15:47
  • @iamsumitd that's just a matter of the order of the `System.out.println()`'s. I've edited my answer to print them out in the order you're expecting them. – Matthew Formosa Oct 20 '19 at 15:50
  • Same problem as I initially had. It has to do with the name formatting. The compiler responds differently in case of full name and just the first name. See this image - https://imgur.com/a/WONFn2b – iamsumitd Oct 20 '19 at 16:10
  • Oh ok, now I see the problem. You will need to opt for a new Scanner when working with Strings, and use another Scanner when not working with Strings. This is a known limitation with Java Scanners. I have edited my answer once again. – Matthew Formosa Oct 20 '19 at 16:28
  • Great! It works fine. Now, I'll go over and probably try to advance the program by making it usable to work to multiple users and add error handling. Thanks. – iamsumitd Oct 20 '19 at 16:36