Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2009
    Posts
    32
    Rep Power
    0

    Default 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;
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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>?

  3. #3
    Join Date
    Sep 2009
    Posts
    32
    Rep Power
    0

    Default

    thanks for the reply i will try it
    thankyou for the help!!

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    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>?
    Using labels can make your loops twice as fast (if you use'm correctly).
    But... in this case, his continue is quite useless. ;)
    I die a little on the inside...
    Every time I get shot.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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");
    }
    ...

  6. #6
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    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...

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What'ya think of this?

    Java 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);
            }
    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 element

    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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:

    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;
            }
    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...
    Last edited by Tolls; 09-24-2009 at 04:51 PM. Reason: Finally figured out what was going on.

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    O.K.

    You beat me. xD Yours is slightly faster. ^^
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    I have absolutely no idea if it's faster (I suspect they're very similar).
    But at least it doesn't have pseudo-gotos in it...
    :)

Similar Threads

  1. Executing a jar file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 02-14-2014, 09:02 AM
  2. Executing .exe from java
    By rp181 in forum Advanced Java
    Replies: 6
    Last Post: 09-21-2009, 03:32 PM
  3. Batch Executing
    By sokolramaj in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2009, 04:17 PM
  4. help-executing the program
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 08-30-2008, 09:18 PM
  5. Problem with executing
    By mcal in forum New To Java
    Replies: 2
    Last Post: 02-09-2008, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •