Connecting Tech Pros Worldwide Forums | Help | Site Map

java loops

Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#1: Aug 15 '09
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..

Expand|Select|Wrap|Line Numbers
  1. 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's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 15 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2. for (int i= 0 i < n; i++)
  3.    sum+= dataTable.getValueAt(i, 4);
  4.  
That's all there is to it ...

kind regards,

Jos
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#3: Aug 15 '09

re: java loops


thank you. But i get an error message when trying to compile. Do you know what this means?? here is my code
Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2.          for (int i= 0; i < Contact.NUM_FIELDS; i++)
  3.           sum += dataTable.getValueAt(i, 4);
Prog3.java:368: operator + cannot be applied to int,java.lang.Object
sum += dataTable.getValueAt(i, 4);
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Aug 15 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

thank you. But i get an error message when trying to compile. Do you know what this means?? here is my code

Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2.          for (int i= 0; i < Contact.NUM_FIELDS; i++)
  3.           sum += dataTable.getValueAt(i, 4);
Prog3.java:368: operator + cannot be applied to int,java.lang.Object
sum += dataTable.getValueAt(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:

Expand|Select|Wrap|Line Numbers
  1. sum+= (Integer)dataTable.getValueAt(i, 4);
  2.  
kind regards,

Jos
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#5: Aug 15 '09

re: java loops


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??

Expand|Select|Wrap|Line Numbers
  1. String total = Integer.toString(sum);
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Aug 16 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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??

Expand|Select|Wrap|Line Numbers
  1. 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
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#7: Aug 16 '09

re: java loops


i still get an error of "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer"

this is my code
Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2.  
  3.          for (int i= 0; i < Contact.NUM_FIELDS; i++)
  4.               sum += (Integer)dataTable.getValueAt(i, 4);
  5.             String total = Integer.toString(sum);
  6.             total1.setText(total);
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Aug 16 '09

re: java loops


On which line is that Exception thrown?

kind regards,

Jos
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#9: Aug 16 '09

re: java loops


line 469 which is

Expand|Select|Wrap|Line Numbers
  1. sum += (Integer)dataTable.getValueAt(i, 4);
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Aug 16 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

line 469 which is

Expand|Select|Wrap|Line Numbers
  1. 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.parseInt( ... ) 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
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#11: Aug 17 '09

re: java loops


I think im still a little confused on what to do
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#12: Aug 17 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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:

Expand|Select|Wrap|Line Numbers
  1. sum += Integer.parseInt((String)dataTable.getValueAt(i, 4)); 
  2.  
You have to convert that sum value back to a String again if you want to store it in your textField:

Expand|Select|Wrap|Line Numbers
  1. textField.setText(""+sum);
  2.  
kind regards,

Jos
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#13: Aug 17 '09

re: java loops


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??
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#14: Aug 17 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#15: Aug 17 '09

re: java loops


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

Expand|Select|Wrap|Line Numbers
  1. int rowIndex = dataTable.getSelectedRow(); 
  2.          int sum= 0;
  3.             for (int i= 0; i < rowIndex; i++)
  4.  
  5.  
  6.             sum += Integer.parseInt((String)dataTable.getValueAt(i, 4));
  7.             String total = Integer.toString(sum);
  8.             total1.setText(total);
  9.  
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#16: Aug 17 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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.println( ... ) is a very good debugger; print everything you don't trust and the funny bug pops up all by itself.

kind regards,

Jos
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#17: Aug 17 '09

re: java loops


ok ..i posted print statements to see what was going on. i did a print statement after
Expand|Select|Wrap|Line Numbers
  1. sum += Integer.parseInt((String)dataTable.getValueAt(i, 4));
  2. System.out.println(sum);
and it prints out 0.

so is the sum+= not working right??
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#18: Aug 17 '09

re: java loops


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

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < dataTable.getRowCount(); i++) {
  2.         int entry = Integer.parseInt( (String) dataTable.getValueAt(i,4));
  3.         sum += entry;
  4.         String total = Integer.toString(sum);
  5.     total1.setText(total);

Thanks for your help!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#19: Aug 18 '09

re: java loops


Quote:

Originally Posted by yeshello54 View Post

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

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < dataTable.getRowCount(); i++) {
  2.         int entry = Integer.parseInt( (String) dataTable.getValueAt(i,4));
  3.         sum += entry;
  4.         String total = Integer.toString(sum);
  5.     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
Reply

Tags
incrementing, java, loop, swing