java loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    java loops

    Hello. So i have a question regarding incrementing a value in a loop.

    I have a data table in my swing program. the data table has 5 columns. In the last column is a integer. I want to be able to grab all the prices in a table of n size. as of right now i just grab the first column's price by doing the following..

    Code:
    Object colIndex = dataTable.getValueAt(0,4);
    how would i go about constructing a loop that adds up every price in that table and stores it in a variable so that i can use it for other things. Thanks any help would be great.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by yeshello54
    Hello. So i have a question regarding incrementing a value in a loop.

    I have a data table in my swing program. the data table has 5 columns. In the last column is a integer. I want to be able to grab all the prices in a table of n size. as of right now i just grab the first column's price by doing the following..

    Code:
    Object colIndex = dataTable.getValueAt(0,4);
    how would i go about constructing a loop that adds up every price in that table and stores it in a variable so that i can use it for other things. Thanks any help would be great.
    I assume that getValueAt( ... ) method takes a row number and a column number parameter. If so getValueAt(i, 4) takes the value of the fifth column (the integer value) from the i-th row. All you have to do is make variable i loop over all values 0 ... n-1 (all 'n' values) and add the corresponding column values in those rows. Something like this:

    Code:
    int sum= 0;
    for (int i= 0 i < n; i++)
       sum+= dataTable.getValueAt(i, 4);
    That's all there is to it ...

    kind regards,

    Jos

    Comment

    • yeshello54
      New Member
      • Mar 2009
      • 54

      #3
      thank you. But i get an error message when trying to compile. Do you know what this means?? here is my code
      Code:
      int sum= 0;
      	 	for (int i= 0; i < Contact.NUM_FIELDS; i++)
      	  	sum += dataTable.getValueAt(i, 4);
      Prog3.java:368: operator + cannot be applied to int,java.lang.O bject
      sum += dataTable.getVa lueAt(i, 4);

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by yeshello54
        thank you. But i get an error message when trying to compile. Do you know what this means?? here is my code
        Code:
        int sum= 0;
        	 	for (int i= 0; i < Contact.NUM_FIELDS; i++)
        	  	sum += dataTable.getValueAt(i, 4);
        Prog3.java:368: operator + cannot be applied to int,java.lang.O bject
        sum += dataTable.getVa lueAt(i, 4);

        That method returns an Object type; you can't add Objects and ints. You have to explicitly cast it to an Integer, like this:

        Code:
        sum+= (Integer)dataTable.getValueAt(i, 4);
        kind regards,

        Jos

        Comment

        • yeshello54
          New Member
          • Mar 2009
          • 54

          #5
          sweet thank you. I guess i have more questions on casting. so once i get the value it is stored in an int. I try to use the setText() on one of my textfields. but this functions parameter is only a string. so when i try to convert the int to a string i get a error that says string cant be cast to int..am i doing this wrong??

          Code:
          String total = Integer.toString(sum);

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by yeshello54
            sweet thank you. I guess i have more questions on casting. so once i get the value it is stored in an int. I try to use the setText() on one of my textfields. but this functions parameter is only a string. so when i try to convert the int to a string i get a error that says string cant be cast to int..am i doing this wrong??

            Code:
            String total = Integer.toString(sum);
            That code snippet seems ok to me; set the text of that field to the 'total' value.

            kind regards,

            Jos

            Comment

            • yeshello54
              New Member
              • Mar 2009
              • 54

              #7
              i still get an error of "java.lang.Clas sCastException: java.lang.Strin g cannot be cast to java.lang.Integ er"

              this is my code
              Code:
              int sum= 0;
              
              	 	for (int i= 0; i < Contact.NUM_FIELDS; i++)
              	  		sum += (Integer)dataTable.getValueAt(i, 4);
              			String total = Integer.toString(sum);
              			total1.setText(total);

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                On which line is that Exception thrown?

                kind regards,

                Jos

                Comment

                • yeshello54
                  New Member
                  • Mar 2009
                  • 54

                  #9
                  line 469 which is

                  Code:
                  sum += (Integer)dataTable.getValueAt(i, 4);

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by yeshello54
                    line 469 which is

                    Code:
                    sum += (Integer)dataTable.getValueAt(i, 4);
                    Ah, does that getValueAt( ... ) method return a String? We (I) assumed it returned an Integer. You have to convert that String to an int by using the static method Integer.parseIn t( ... ) then and forget about the cast to an Integer type, i.e. first cast it to a String and then apply that method.

                    kind regards,

                    Jos

                    Comment

                    • yeshello54
                      New Member
                      • Mar 2009
                      • 54

                      #11
                      I think im still a little confused on what to do

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by yeshello54
                        I think im still a little confused on what to do
                        If that getValueAt( ... ) method returns a String (check that) you have to convert it to an int before you can add the value to your sum variable:

                        Code:
                        sum += Integer.parseInt((String)dataTable.getValueAt(i, 4));
                        You have to convert that sum value back to a String again if you want to store it in your textField:

                        Code:
                        textField.setText(""+sum);
                        kind regards,

                        Jos

                        Comment

                        • yeshello54
                          New Member
                          • Mar 2009
                          • 54

                          #13
                          im must be tired..so i was not paying attention but my getValueAt() returns an object not a string. so does that make things different??

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by yeshello54
                            im must be tired..so i was not paying attention but my getValueAt() returns an object not a string. so does that make things different??
                            The String class extends from the Object class so a String is an Object; that's how object oriented programming works. If that method returns a String (and I'm about sure it does, given the error messages) but tells you it is returning an Object it isn't lying to you but you have to cast it back to the String type and that's exacly what my first code snippet does.

                            kind regards,

                            Jos

                            Comment

                            • yeshello54
                              New Member
                              • Mar 2009
                              • 54

                              #15
                              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..

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

                              Comment

                              Working...