Can not get loop to count

Please help. I have seen tons of posts similar to this but have not been able to find the solution. A simple bit of code that works in Perl, C#, C++, but not Java.

int i = 1;

if (grp !=null){

bwGrp.write(i +": " + grp +"\r\n");

i++;

}

Out put is ALWAYS 1: groupname

I would like it to count so that I know how many lines are written.

Thanks,

Elliott

[615 byte] By [nilSatisa] at [2007-10-3 3:34:27]
# 1
I haven't got it...are you expecting an 'if' to iterate like 'while'?
lfschucka at 2007-7-14 21:28:59 > top of Java-index,Java Essentials,New To Java...
# 2
No the condition of the loop is fine. I just want a way to print the line number to my text file.
nilSatisa at 2007-7-14 21:28:59 > top of Java-index,Java Essentials,New To Java...
# 3

> Please help. I have seen tons of posts similar to

> this but have not been able to find the solution. A

> simple bit of code that works in Perl, C#, C++, but

> not Java.

>

> >int i = 1;

> if (grp != null){

>

> rp.write(i + ": " + grp + "\r\n");

> i++;

>

>

> Out put is ALWAYS 1: groupname

> I would like it to count so that I know how many

> lines are written.

> Thanks,

> Elliott

It's not a loop.

while (grp != null) {

rp.write(i + ": " + grp + "\r\n");

i++;

}

will result in either no print or an endless loop, because nothing about gpr is changing. Perhaps you want:

for (int i = 0; i < grp.length; i++) {

if ( grp[i] != null )

rp.write(i + ": " + grp[i] + "\r\n");

}

abillconsla at 2007-7-14 21:28:59 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks abillconsl Your right, my brain was not working properly, too much coding driving me insane!I put the increment in the proper location and it works fine...
nilSatisa at 2007-7-14 21:28:59 > top of Java-index,Java Essentials,New To Java...
# 5
Welcome.
abillconsla at 2007-7-14 21:28:59 > top of Java-index,Java Essentials,New To Java...