Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding numbers through a loop

Status
Not open for further replies.

Kruzer

Programmer
Jun 16, 2000
117
US
I'm drawing a blank on how to add numbers up while looping through a row of numbers.

The values are 16 + 2 + 1 + 2 + 1 = 22

How do I add the numbers up to equal 22?

for(int x = 2; x <= Sheet1.getLastRow(); x++)
{
for(int i = 5; i <= 16; i++)
{
addValues = Sheet1.getText(x, i); //(row, col, value)
// addValues will loop through getting values 16,2,1,2,1
????
}
// adds the total to the 3 column
Sheet1.setText(x, 3, addValues);
}
Dano
dskryzer@hotmail.com
What's your major malfunction
 
If I understand you correctly than you want:
Code:
int addValues = 0;
for(int x = 2; x <= Sheet1.getLastRow(); x++) {

   for(int i = 5; i <= 16; i++) {
      addValues  = addValues + Sheet1.getText(x, i);
   }
   // adds the total to the 3 column

   Sheet1.setText(x, 3, addValues);
}
Wushutwist
 
here's what I tried

The number value is a string so I had to convert the number to int. Also .setText(row,col,value) needs a string value so coverted the int back to a string.

int temp = 0;
for(int sheet1RowCnt = 2; sheet1RowCnt <= Sheet1.getLastRow(); sheet1RowCnt++)
{
for(int i = 5; i <= 16; i++)
{
addValues = Sheet1.getText(sheet1RowCnt, i);
if(addValues != &quot;&quot;)
{
temp = temp + Integer.parseInt(Sheet1.getText(sheet1RowCnt, i));
addValues = Integer.toString(temp);
}
else
{
System.out.println(&quot;Null&quot;);
}
}
Sheet1.setText(sheet1RowCnt, 3, addValues);
}
Dano
dskryzer@hotmail.com
What's your major malfunction
 
oops forgot the error message to add the thread

java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java, Compiled Code)
at java.lang.Integer.parseInt(Integer.java, Compiled Code)
...
Dano
dskryzer@hotmail.com
What's your major malfunction
 
Is this a Compile Error or a Runtime Error?

If Compile-Time:
Add a try catch block around your Integer.parseInt() statements.

If Runtime:
The String you are trying to convert is not a valid number. Add in some println statements so that you can see the exact Strings you are trying to convert to int and I am sure you will find the culprit for your problems.

Wushutwist
 
Sounds like your

temp = temp + Integer.parseInt(Sheet1.getText(sheet1RowCnt, i));

line is retrieveing a non numeric character.
 
Seems like there are a few errors. Try this instead:-

int temp = 0;
for(int sheet1RowCnt = 2; sheet1RowCnt <= Sheet1.getLastRow(); sheet1RowCnt++)
{
for(int i = 5; i <= 16; i++)
{
String addValues = Sheet1.getText(sheet1RowCnt, i);
if(!addValues.trim().equals(&quot;&quot;))
{
try
{
temp = temp + Integer.parseInt(addValues);
}
catch (NumberFormatException e)
{
System.out.println(&quot;Invalid value : &quot;+addValues);
System.out.println(&quot;Error : &quot;+e);
}
}
}
Sheet1.setText(sheet1RowCnt, 3, &quot;&quot;+temp);
}

This should be able to tell you which part of your Sheet1 has error.

Regards,
Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top