Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: do-while help!!!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Angry do-while help!!!

    This is my Complete bank account program.
    import javax.swing.JOptionPane;
    public class BankAcct 
    {// start of class
        private String clientName;
        private double BankMoney;
        private double Transaction;
     
        double kth;
        private String png;
        private String ptr;
     
     
     
        public String Getclientname(){
            return clientName;
        }
     
        public double GetBankMoney(){
            return BankMoney;
        }
     
        public double GetTransaction(){
            return Transaction;
        }
     
        public String Getpng(){
            return png;
        }
     
        public String Getptr(){
            return ptr;
        }
     
        public void SetclientName(String clientName){
            this.clientName = clientName;
        }
     
        public void SetBankMoney(double BankMoney){
            this.BankMoney = BankMoney;
        }
     
        public void SetTransaction(double Transaction){
            this.Transaction = Transaction;
        }
     
        public void Setpng(String png){
            this.png = png;
        }
     
        public void Setptr(String ptr){
            this.ptr = ptr;
        }
     
        public static void main(String[] args){// start of main
            BankAcct inf = new BankAcct();
            String ft,wh = "";
            inf.Setpng(JOptionPane.showInputDialog("Do you want to create a bank account[Yes][No]"));
            do{ //Start of outer do
    if("Yes".equalsIgnoreCase(inf.png))
    { // start of if
            inf.SetclientName(JOptionPane.showInputDialog("Enter your Name: "));
            ft =JOptionPane.showInputDialog("Enter your account Number: ");
            inf.BankMoney = Double.parseDouble(JOptionPane.showInputDialog("Enter the money to put into your bank account"));  
            inf.kth=Double.parseDouble(JOptionPane.showInputDialog(null,"your account Name: "+inf.clientName
                    +"\nAccount Number: ***** " + "\n Your Balance: "+inf.BankMoney+"\nPress[1]"
                    +"to Withdraw \nPress[2] to Deposit \nPress [3] to Exit"));
    } // end of if
            else return; 
     
     
           switch((int)inf.kth)
    { //start of switch
     
               case 1:
                   double db;
                   double bd;
     
                  do{ //start of inner do
                  db =Double.parseDouble(JOptionPane.showInputDialog("Enter the money to Withdraw: "));
                   bd = inf.BankMoney -db ;
                   if(bd<0){//start of if
     inf.ptr=JOptionPane.showInputDialog("Overwithdrawing!!Try Again?[yes][no]");
    }//end of if
                   else if("NO".equalsIgnoreCase(inf.ptr))return;
                   else JOptionPane.showMessageDialog(null,"Starting Money: "+inf.BankMoney+"\nMoney Withdraw: "+db+"\nCurrent Savings: " + bd);
                  } //end of inner do
                   while("Yes".equalsIgnoreCase(inf.ptr));
                   break;
     
               case 2:
                   double bb;
                   double cv;
                   bb =Double.parseDouble(JOptionPane.showInputDialog("Enter the money to deposit: "));
                   cv = bb + inf.BankMoney;
                   JOptionPane.showMessageDialog(null,"Starting Money: "+inf.BankMoney+"\nMoney deposited: "+ bb+"\ncurrent Savings: "+ cv);
                   break;
               case 3:
                   JOptionPane.showMessageDialog(null, "Exiting! Thank your for using our program");
                   break;
               default:
                  wh=JOptionPane.showInputDialog("Sorry Wrong Number!Try Again?[yes][no]");
     
    }// end of switch
            } //end of outer do
    while("Yes".equalsIgnoreCase(wh));
     
        }// end of class
    }//end of main



    Im having error on this part
    switch((int)inf.kth){
     
               case 1:
                   double db;
                   double bd;
            do{
                   db =Double.parseDouble(JOptionPane.showInputDialog("Enter the money to Withdraw: "));
                   bd = inf.BankMoney -db ;
                   if(bd<0) inf.ptr=JOptionPane.showInputDialog("Overwithdrawing!!Try Again[yes][no]");
                   else if("NO".equalsIgnoreCase(inf.ptr))return;
                   else JOptionPane.showMessageDialog(null,"Starting Money: "+inf.BankMoney+"\nMoney Withdraw: "+db+"\nCurrent Savings: " + bd);
    }
                   while("Yes".equalsIgnoreCase(inf.ptr));
                   break;

    can someone tell me what s wrong in this code??? when i run my bank account program and trigger the overwithdrawing and type yes it loops and loops even i don't overdraw(means bd>=0). please help me to solve this problem.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    it loops and loops even i don't overdraw
    What condition causes the code to loop? Is that condition true or false?
    If it's true why is it true? Should the condition be false? What will make the condition be false?
    Print out the value of inf.ptr so you can see what the computer is seeing.

    The code is poorly formatted which makes it hard to read. You should use {}s with if and else statements so it is easy to see what code is included in them.
    }s should not be hidden at the end of statements. They should be in a line by themselves and inline below the start of the statement with the pairing {
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    when i put a value(for example my money in the bank that i put is 1000 and i withdraw 1500 then the inf.ptr dialog will appear)and if i type yes in the dialogbox the db =Double.parseDouble(JOptionPane.showInputDialog("E nter the money to Withdraw: ")); will ask me again to enter an amount to withdraw if i put 500 the else JOptionPane.showMessageDialog(null,"Starting Money: "+inf.BankMoney+"\nMoney Withdraw: "+db+"\nCurrent Savings: " + bd); will display and expected the program to be terminated but it loops again and ask me to enter an amount to withdraw.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    Please edit the posted code and fix the formatting so it can be read.

    it loops again
    What is the value of inf.ptr when you print it out? Print its value before the loop as well as inside the loop so you can see what the computer sees when it executes the code.

    Also print the value of bd after its value is changed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    This is the output
    Starting Money: 1000
    Withdraw: 500
    Current Savings: 500

    if i don't trigger the inf.ptr=JOptionPane.showInputDialog("Overwithdrawi ng!!Try Again[yes][no]"); the program doesn't loop but when i trigger it and i type yes in the dialogbox that will start the looping the inf.ptr=JOptionPane.showInputDialog("Overwithdrawi ng!!Try Again[yes][no]");
    you can try my full bank account to know what im saying.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    What is the value of inf.ptr when you print it out? Print its value before the loop as well as inside the loop so you can see what the computer sees when it executes the code.

    Also print the value of bd after its value is changed.

    Please edit the posted code and fix the formatting so it can be read.


    The posted code does not compile. It is full of errors.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    (I edit the code you can try it again i hope it will work now, im using netbeans.)
    i compile the code in another package and it works fine. btw when inf.ptr value is "yes" the program will ask me a money to withdraw then it will display
    Starting Money: 1000
    Withdraw: 500
    Current Savings: 500
    then it loops again and ask me a money to withdraw.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    The formatting still needs fixing.
    Nothing should follow a { on the same line.
    } should be on a line by itself and aligned below the statement with the pairing {
    Statements within loops and if statements should be indented.
    if statements should enclose the following statements in {}s


    The code does not print out the value of inf.ptr so you can see what it is!!!!
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    ok i fix the curly braces and separate them.
    PS: i put comment on every curly braces to be more understandable.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    What about printing inf.ptr?

    What about {}s for all the if statements?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Jesirism (January 18th, 2013)

  12. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    i put {} on all if statements but same result
    i uploaded a video heres the link: Video0019 - YouTube
    as you can see at the last part of the video the program keeps looping even the value of withdraw is lower than than the value of Starting money.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    What was the value of inf.ptr when you print it out?
    Print it out before the do{ and before the }while(

    i put {} on all if statements but same result
    Adding {}s shouldn't change the way the code executes, but it will make the code easier to read and understand.

    The formatting is still wrong. There should not be so many statements starting in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Jesirism (January 18th, 2013)

  15. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: do-while help!!!

    I think i need more understanding about do-while(sorry im newbie at programming)so i will use if else statement instead of do while
    switch((int)inf.kth){

    case 1:
    double db;
    double bd;

    db =Double.parseDouble(JOptionPane.showInputDialog("E nter the money to Withdraw: "));
    bd = inf.BankMoney -db;
    if(bd<0) {
    JOptionPane.showMessageDialog(null,"Overwithdrawin g!!");
    }
    else JOptionPane.showMessageDialog(null,"Starting Money: "+inf.BankMoney+"\nMoney Withdraw: "+db+"\nCurrent Savings: " + bd);
    break;

    the code produce the output that i wanted.
    btw thanks for the help man

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: do-while help!!!

    What was the value of inf.ptr when you print it out?
    Print it out before the do{ and before the }while(

    Is the above invisible? I've asked that question many times.
    If you don't understand my answer, don't ignore it, ask a question.