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 2 of 2

Thread: Adding and subtracting problem

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Adding and subtracting problem

    Hey everyone, I need a little help with my budget program. Here is my situation, I have 2 tabs in a GUI, one tab adds a transactions when the add button is clicked, and in the other tab displays a table showing all the transactions. In my code, I want it so that when the user chooses a deposit(combo box variable name = cbType, indexnumber for deposit is 0) it will add to the total and when the user chooses withdraw(index number is 1) then it will subtract it from the total. Here is the code.... (note as well, the code also adds a new row to the table)

    //add button clicked
    	private class BtnAddActionListener implements ActionListener {
    		public void actionPerformed(ActionEvent arg0) {
    			((DefaultTableModel)table.getModel()).addRow(new Object[]{
    					cbMonth.getSelectedItem() + "/" + txtDay.getText() + "/" + cbYear.getSelectedItem(),
    					cbCategory.getSelectedItem(),
    					txtItem.getText(),
    					cbType.getSelectedItem(),
    					txtAmount.getText()});
    			// calculates the total
    			Double summl = 0.0;
     
    		    for(int i = 0; i < table.getRowCount(); i++){
    		    	if(cbType.getSelectedIndex() == 0){
    		    		summl=summl+Double.parseDouble(table.getValueAt(i,4).toString());
    		    	}else {
    		    		summl=summl-Double.parseDouble(table.getValueAt(i,4).toString());
    		    	}
     
    		    }  
    		    //displays the total
    		    toString();
    			moneyTotal.setText(String.valueOf(summl));

    So when I tested the program with 2 transactions, the first transaction was a deposit and the 2nd transaction was a withdraw. The end product was that both amounts were subtracted from the total. When I did a withdraw first and a deposit second, the amounts were both added together.

    Any help would be very grateful thanks!!


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

    Default Re: Adding and subtracting problem

    Try doing some debugging by adding some println statements that print out the values of variables as they are changed and used to see what the code is doing when it executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with subtracting a negative fraction
    By Chassy13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2014, 12:52 AM
  2. Adding and Subtracting positive or negative two numbers of any length with strings
    By javabeginner63 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 5th, 2014, 05:11 PM
  3. Subtracting each element from an array
    By Tronez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 15th, 2011, 04:41 PM
  4. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  5. Problem with adding to a list in a loop
    By aminmusa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 30th, 2010, 10:13 AM