Results 1 to 10 of 10
Thread: for loop not executing
- 09-23-2009, 08:54 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 32
- Rep Power
- 0
for loop not executing
hi all i am trying the following code but for loop is not working with label and without label it is working fine.
Please let me know what mistake i am doing
outer:for(int i=0;i<sheet.getRows();i++)
{
Subscriber subscriber=new Subscriber();
for(int j=0;j<sheet.getColumns();j++)
{
Cell cell=sheet.getCell(j,i);
CellType type=cell.getType();
st=cell.getContents();
if(st!=null && (j==0)&& !st.contains("@"))
{
subscriber.setName(st.trim());
}
else
{
validationDisplay.append("Error on Row :"+i+" Column :"+j+", ");
continue outer;
}
}
- 09-23-2009, 09:44 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
I've never used labels.
There's usually no need for them.
What does "is not working" actually mean?
Why not get rid of the label and simply use "break" instead of "continue" <label>?
- 09-23-2009, 09:48 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 32
- Rep Power
- 0
thanks for the reply i will try it
thankyou for the help!!
- 09-24-2009, 01:47 PM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
- 09-24-2009, 02:05 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Twice as fast?
I somehow doubt that's the case, except maybe in the following:
...Java Code:boolean myflag = true; mylabel: for (int i = 0; i < 10; i++) { if (myflag) continue mylabel; System.out.println("I'm never going to get here"); }
- 09-24-2009, 02:08 PM #6
your example rox :p "i'm never going to get here"
Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...
- 09-24-2009, 03:36 PM #7
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
What'ya think of this?
Without the continue you would have to add an if statement to check if for the 2 lines after the inner loop, you would have to use some kind of boolean and set it to true for the if after the inner loop. Next to that, if you find a correct value, you would still loop through the rest of the 'possible' matches (Assuming you don't use a break). On an average, you're next match will be in the middle, so you only have to loop to that, which is faster, than iterate through every elementJava Code:final String[] sarray = {"A","B","C","A","A","B","D","E"}; ArrayList<Integer> resultI = new ArrayList<Integer>(); ArrayList<String> resultS = new ArrayList<String>(); L1: for (int i = 0; i < sarray.length; ++i) { for (int j = 0; j < resultS.size(); ++j) { if (sarray[i].equals(resultS.get(j))) { resultI.add(j); continue L1; } } resultS.add(sarray[i]); resultI.add(resultS.size()-1); }
So it is faster, and it saves some memory, using a label. (just a boolean, but who cares, xD)I die a little on the inside...
Every time I get shot.
- 09-24-2009, 04:31 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
It might be faster and all that, but since I haven't the faintest what it's actually doing it's no more clever than my example.
ETA: Ah, I think I see now. So here you go:
No labels, no overhead of ArrayList<Integer>, and all the implicit conversions therein, no internal loop (at least not by me) relying instead on the underlying logic of my List...Java Code:final String[] sarray = {"A","B","C","A","A","B","D","E"}; List<String> stringlist = new ArrayList<String>(); int[] locations = new int[sarray.length]; for (int i = 0; i < sarray.length; ++i) { int j = stringlist.indexOf(sarray[i]); if (j == -1) { stringlist.add(sarray[i]); j = stringlist.size() - 1; } locations[i] = j; }Last edited by Tolls; 09-24-2009 at 04:51 PM. Reason: Finally figured out what was going on.
- 09-24-2009, 06:24 PM #9
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
O.K.
You beat me. xD Yours is slightly faster. ^^I die a little on the inside...
Every time I get shot.
- 09-25-2009, 09:22 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Executing .exe from java
By rp181 in forum Advanced JavaReplies: 6Last Post: 09-21-2009, 03:32 PM -
Batch Executing
By sokolramaj in forum Advanced JavaReplies: 1Last Post: 04-22-2009, 04:17 PM -
help-executing the program
By j2vdk in forum New To JavaReplies: 6Last Post: 08-30-2008, 09:18 PM -
Problem with executing
By mcal in forum New To JavaReplies: 2Last Post: 02-09-2008, 01:51 PM -
Executing a jar file
By peiceonly in forum New To JavaReplies: 2Last Post: 04-06-2007, 02:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks