java loops | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| |
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.. - 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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.. - 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: -
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
| | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| | | 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 - 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.Object
sum += dataTable.getValueAt(i, 4);
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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 - 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.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: -
sum+= (Integer)dataTable.getValueAt(i, 4);
-
kind regards,
Jos
| | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| | | 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?? - String total = Integer.toString(sum);
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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?? - 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
| | | 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 - 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);
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops
On which line is that Exception thrown?
kind regards,
Jos
| | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| | | re: java loops
line 469 which is - sum += (Integer)dataTable.getValueAt(i, 4);
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
Originally Posted by yeshello54 line 469 which is - 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
| | | re: java loops
I think im still a little confused on what to do
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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: -
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: -
textField.setText(""+sum);
-
kind regards,
Jos
| | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| | | 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??
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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
| | Member | | Join Date: Mar 2009 Location: San Diego , CA
Posts: 45
| | | 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.. -
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);
-
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
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.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
| | | re: java loops
ok ..i posted print statements to see what was going on. i did a print statement after - sum += Integer.parseInt((String)dataTable.getValueAt(i, 4));
-
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
| | | re: java loops
actually i just figured it out..i fixed it with the following: - 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!
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: java loops Quote:
Originally Posted by yeshello54 actually i just figured it out..i fixed it with the following: - 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|