java loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by yeshello54
    ok i got the error to stop..now when it displays the total it just gives a 0.
    the values i had were 50 and 40..
    System.out.prin tln( ... ) is a very good debugger; print everything you don't trust and the funny bug pops up all by itself.

    kind regards,

    Jos

    Comment

    • yeshello54
      New Member
      • Mar 2009
      • 54

      #17
      ok ..i posted print statements to see what was going on. i did a print statement after
      Code:
      sum += Integer.parseInt((String)dataTable.getValueAt(i, 4));
      System.out.println(sum);
      and it prints out 0.

      so is the sum+= not working right??

      Comment

      • yeshello54
        New Member
        • Mar 2009
        • 54

        #18
        actually i just figured it out..i fixed it with the following:

        Code:
        for(int i=0; i < dataTable.getRowCount(); i++) {
            	int entry = Integer.parseInt( (String) dataTable.getValueAt(i,4));
            	sum += entry;
           	 String total = Integer.toString(sum);
        	total1.setText(total);

        Thanks for your help!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by yeshello54
          actually i just figured it out..i fixed it with the following:

          Code:
          for(int i=0; i < dataTable.getRowCount(); i++) {
              	int entry = Integer.parseInt( (String) dataTable.getValueAt(i,4));
              	sum += entry;
             	 String total = Integer.toString(sum);
          	total1.setText(total);

          Thanks for your help!
          I don't understand why that would help, i.e. the parseInt( ... ) method returns an int that can be directly added to the 'sum' variable, no need for a temporary variable. btw, your indentation stinks. That 'setText( ... )' method shouldn't be in the body of that loop.

          kind regards,

          Jos

          Comment

          Working...