Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Question

  • 07-07-2011 06:10PM
    #1
    Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭


    Hi Im currently learning java and I am making a small bank account program.

    Im having a small problem tho. When i create a new account my withdraw money and lodge money methods stop working.

    Im storing the accounts in a Vector. My create account method is in main. the withdraw and lodge methods are in another class. here is some of the main method. I can show you other methods if you need to see em but im pretty sure they are ok.

    while(!quit){
    choice=newMenu.menuShow();
    if(choice==1){ //just to display account
    newBooks.displayAccounts();
    }
    else if(choice==2){
    account a=createAccount(); //create new account
    newBooks.addAccount(a); //add account to vector
    }
    else if(choice==3){
    System.out.println("Enter name of account");
    String name=in.nextLine();
    newBooks.deleteAccount(name);
    }
    else if(choice==4){ //this is where the trouble starts.
    System.out.println("Enter name of account"); //only after i create a new account tho
    String name=in.nextLine(); //it just prints enter name and skips the in.nextLine() command
    boolean ok=newBooks.lodgeMoney(name); //lodgeMoney() gets amount to be lodged and checks if account entered exists and returns true
    if(!ok){ //because it skips the in.nextLine() its sayin the account does not exist
    System.out.println("No account");
    }




    this only happens after i create a new account. I have accounts in at the start and i can lodge money and withdraw from them no problem.


Comments

  • Registered Users, Registered Users 2 Posts: 297 ✭✭stesh


    jonny666 wrote: »
    Hi Im currently learning java and I am making a small bank account program.

    Im having a small problem tho. When i create a new account my withdraw money and lodge money methods stop working.

    Im storing the accounts in a Vector. My create account method is in main. the withdraw and lodge methods are in another class. here is some of the main method. I can show you other methods if you need to see em but im pretty sure they are ok.

    while(!quit){
    choice=newMenu.menuShow();
    if(choice==1){ //just to display account
    newBooks.displayAccounts();
    }
    else if(choice==2){
    account a=createAccount(); //create new account
    newBooks.addAccount(a); //add account to vector
    }
    else if(choice==3){
    System.out.println("Enter name of account");
    String name=in.nextLine();
    newBooks.deleteAccount(name);
    }
    else if(choice==4){ //this is where the trouble starts.
    System.out.println("Enter name of account"); //only after i create a new account tho
    String name=in.nextLine(); //it just prints enter name and skips the in.nextLine() command
    boolean ok=newBooks.lodgeMoney(name); //lodgeMoney() gets amount to be lodged and checks if account entered exists and returns true
    if(!ok){ //because it skips the in.nextLine() its sayin the account does not exist
    System.out.println("No account");
    }




    this only happens after i create a new account. I have accounts in at the start and i can lodge money and withdraw from them no problem.

    Well first of all, what you posted seems to be syntactically incorrect: the while block and the final else if block aren't terminated. I presume you just missed the last couple of lines when you copy-pasted. Anyway, you'll need to post more than that if anyone is to be able to figure out what's going on.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    ok i have actually managed to fix the problem.

    i declared final Static scanner in=new Scanner(System.in); outside the main method thinkin it would save me time not having to declare a scanner every time but I got rid of it and declared the scanner in each method instead and it fixed the problem.

    Can someone tell me tho why it fixed it?


    Edit:Stesh just seen your post. ya i didnt copy and paste the whole thing there were a few other options in there.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    jonny666 wrote: »
    ok i have actually managed to fix the problem.

    i declared final Static scanner in=new Scanner(System.in); outside the main method thinkin it would save me time not having to declare a scanner every time but I got rid of it and declared the scanner in each method instead and it fixed the problem.

    Can someone tell me tho why it fixed it?


    Edit:Stesh just seen your post. ya i didnt copy and paste the whole thing there were a few other options in there.

    sounds like scanner was out of scope


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    sounds like scanner was out of scope

    what exactly does this mean??


  • Registered Users, Registered Users 2 Posts: 2,088 ✭✭✭henryporter


    Means that the methods can't see the scanner object that you have created - an option would be to pass the scanner object as a parameter to the method to avoid having to create a new scanner each time.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    Means that the methods can't see the scanner object that you have created - an option would be to pass the scanner object as a parameter to the method to avoid having to create a new scanner each time.



    brill. Il try that and see what happens. thanks!


Advertisement