473,487 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

java loops

54 New Member
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.
Aug 15 '09 #1
18 2798
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 15 '09 #2
yeshello54
54 New Member
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);
Aug 15 '09 #3
JosAH
11,448 Recognized Expert MVP
@yeshello54

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
Aug 15 '09 #4
yeshello54
54 New Member
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);
Aug 15 '09 #5
JosAH
11,448 Recognized Expert MVP
@yeshello54
That code snippet seems ok to me; set the text of that field to the 'total' value.

kind regards,

Jos
Aug 16 '09 #6
yeshello54
54 New Member
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);
Aug 16 '09 #7
JosAH
11,448 Recognized Expert MVP
On which line is that Exception thrown?

kind regards,

Jos
Aug 16 '09 #8
yeshello54
54 New Member
line 469 which is

Expand|Select|Wrap|Line Numbers
  1. sum += (Integer)dataTable.getValueAt(i, 4);
Aug 16 '09 #9
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 16 '09 #10
yeshello54
54 New Member
I think im still a little confused on what to do
Aug 17 '09 #11
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 17 '09 #12
yeshello54
54 New Member
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??
Aug 17 '09 #13
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 17 '09 #14
yeshello54
54 New Member
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.  
Aug 17 '09 #15
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 17 '09 #16
yeshello54
54 New Member
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??
Aug 17 '09 #17
yeshello54
54 New Member
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!
Aug 17 '09 #18
JosAH
11,448 Recognized Expert MVP
@yeshello54
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
Aug 18 '09 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

114
9682
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
5
3223
by: Robert Oschler | last post by:
Preamble: - I know this is the Python forum - I know about (and have used) Jython I already posted this question in comp.lang.java. But after a week I have still not received a single reply....
1
2110
by: pawel | last post by:
I have made some comparision C# to Java RegularExpression. The problem was to find out if the rule match some text. Matching were done for precompiled regular expressions, in 100000 iterations...
133
8420
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
17
4317
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a...
13
4254
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
14
5652
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
4
3497
by: danbuttercup | last post by:
Hi everyone I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start. ...
350
11441
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.