Results 1 to 10 of 10
  1. #1
    theone3nu is offline Member
    Join Date
    Oct 2008
    Posts
    17
    Rep Power
    0

    Default Plz solve this....

    hi Frns.. here r the lines of java code.... i want to jump directly from line 10 to line 17 without being executed those 11 to 16 lines .....help me pls...how to do this...

    for(int i = 0; i < 10; i++){
    .... line 1 to 10
    }

    11 to 16....

    17. System.out.println("hi");

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    don't use a for loop. The loop is designed to run everything in it how ever many times you tell it to. Use a while loop instead.

    If you insist on using a for loop you have to have an if statement that checks some condition and breaks when it's met.
    Java Code:
    for(int i = 0; i < 10; i++){
    //do some stuff
         if(some condition is met){
            break;
         }
    }
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Gogoras is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default

    You may put lines 11 - 16 into an if statement with some condition.

    Bye!

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default da method

    or... you can put the statements on line 17 in a method and that way you can call method from any place in your code.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by theone3nu View Post
    hi Frns.. here r the lines of java code.... i want to jump directly from line 10 to line 17 without being executed those 11 to 16 lines .....help me pls...how to do this...

    for(int i = 0; i < 10; i++){
    .... line 1 to 10
    }

    11 to 16....

    17. System.out.println("hi");
    Since form 11 to 17 are outside of the for loop, why are you worrying about 11 to 16. Put the condition on line 17 and do the stuff.

  6. #6
    theone3nu is offline Member
    Join Date
    Oct 2008
    Posts
    17
    Rep Power
    0

    Default thanks

    hey thanks all for ur replies... actually my problem is not as simple as one statement to use if satements... ok now herez my similar question..which is exact to my problem..
    1.public void keyPressed(KeyEvent e){
    2. if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    3. System.out.println("HI");
    4. System.out.println("Right Key Pressed");
    5. }
    6. }

    here if i press the right key first time it should print only HI then after second time pressing the right key then it should print only Right Key Pressed..This can be done in c/c++ by using goto statement directly..but in java how to do this....pls tell me the solution...

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    add boolean variable to it

  8. #8
    theone3nu is offline Member
    Join Date
    Oct 2008
    Posts
    17
    Rep Power
    0

    Default

    hey where to add boolean variable ...pls put ur code....thanku....

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    global boolean variable with false/true value as you like

    if false/true, exe line 3
    if true/false, exe line 4
    add boolean = !boolean in later code...
    Last edited by mtyoung; 12-23-2008 at 09:28 AM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Solve my program
    By Mika in forum New To Java
    Replies: 7
    Last Post: 01-21-2009, 06:20 AM
  2. please I need your help to solve these problems
    By edge_5050@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 12-17-2008, 08:34 PM
  3. Solve my Problem
    By kyo in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 02:22 PM
  4. Help me to solve problem
    By mansoorhacker in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 08:15 AM
  5. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 05:26 AM

Posting Permissions

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