-1

I'm getting

< invalid assignment operator

in this code snippet

int length=0;
int i=0;
for ( Iterator<String> t_Number = keyset.iterator(); t_Number.hasNext()&& i<length;)
{
  String key = (String)t_Number.next();
  row = sheet.createRow(rownum++);
  Object[] objArr = (Object[])excel_data.get(key);
  int cellnum = 0;
  Object[] array;
  length = (array = objArr).length; continue;
  Object obj = array[i];
  Cell cell = row.createCell(cellnum++);
  if ((obj instanceof Double)) {
    cell.setCellValue(((Double)obj).doubleValue());
  } else {
    cell.setCellValue((String)obj);
  }
  i++;
}

Can some one please let me know to fix this?

3 Answers3

0

The structure of a for loop is:

for (initialization; termination; increment)

Your increment step is i < length, which isn't a statement which modifies anything.

Assuming you want to check for that condition as well, you should use && instead of putting it in the increment step:

for(Iterator<String> t_Number = keyset.iterator(); 
    t_Number.hasNext() && i<length;/* no increment */)

If you do this, make sure you do something inside the loop (or add an actual increment step) which changes keyset or i so that the termination condition will eventually be false.

In response to your update, the new problem comes from this line:

length = (array = objArr).length; continue;

continue means to skip the rest of that repetition of the loop; it will just increment, go back to the start of the block, and keep running. Since you call continue unconditionally, it will always be executed, and nothing in the loop after it can ever be reached. Either remove the continue, or put an if(/* some condition */) before it.

resueman
  • 10,572
  • 10
  • 31
  • 45
0

In the last of the 3 portions of the if statement, Java expects a statement, but all you have is an expression, i<length. The compiler error I get is "not a statement".

If you intended this to be another condition to keep iterating, then add it to the condition (middle) portion.

for (Iterator<String> t_Number = keyset.iterator(); t_Number.hasNext() &&  i<length; )

(It's assumed that something in the actual body of your code is calling t_Number.next() and moving forward in the iteration.)

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Your

i<length

is unnecessary.

Have a read of Ways to iterate over a list in java.

Community
  • 1
  • 1
James K
  • 89
  • 1
  • 10