Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question

Options
  • 07-07-2011 6:10pm
    #1
    Registered Users 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 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 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 Posts: 3,500 ✭✭✭Drexel


    sounds like scanner was out of scope

    what exactly does this mean??


  • Registered Users Posts: 2,089 ✭✭✭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 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