Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Start code again?

  1. #1
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Start code again?

    Assuming that Java is done in a linear form, how can I start a code from the beginning?
    for example, say I have a set of Password Fields, and if any is gotten wrong, the whole code starts again from the beginning.

    if (!passed) {
    //Start from beginning
    }
    return passed;
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    If the tests are in a while(true) loop, issue a continue; to start at the beginning of the loop.

  3. #3
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Start code again?

    Is that the only way to do it? Just out of interesst. Is there no way just to say "If this happens, start again." iunstead of being in a while loop? Because surely that would meen do everything and then start again? I'm just a bit confused.
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    start again.
    Where in the code do you go to start again? What are the all the steps that you are doing? Where in that list of steps do you want to go to start again?
    I thought your code was to test field1 then test field2 then test field3 etc. When there is a problem, you output an error message and go back to the beginning: test field1, then 2 etc

  5. #5
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Start code again?

    Yes.
    1. Password Field 1
    Passed: go to Password Field 2
    failed: restart from Password Field 1
    2. Password Field 2
    Passed: go to Password Field 3
    failed: restart from Password Field 1
    3. Password Field 3
    Passed: go to Password Field 4
    Failed: restart from Password Field 1
    etc...
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    That logic looks like it'd fit in a while(true) loop.
    The code in the loop would continue to the next test as each test passed.
    When a test failed, use the continue statement to start at the beginning;
    At the end of the tests, use a break to exit the loop

  7. #7
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Start code again?

    Thanks! So what would be the code for that?
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    fist line would be the while(true) {

    // then lines of tests with continue when test fails

    } // end of while(true)

  9. #9
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Start code again?

    I've got a book on Java, and it mention while loops but said nothing on while(true) loops (unless they're the same thing), nor continue statements. help?
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    What does your book say about the while statement? What goes in the () following the while? a Condition. What is a condition: an expression that evaluates to true or false. true is an expression that evaluates to true.

    If it doesn't have the continue statement, its missing some important stuff. I wonder what else is missing.

  11. #11
    Junior Member mortalc's Avatar
    Join Date
    May 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Start code again?

    Could you give a quick example on how to do it in this specific case?
    (o o)
    {''''''}
    He has come for you.
    There is no escaoe.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Start code again?

    while (true) {
      if(!test1_OK)
        continue;    // go back to start
      if(!test2_OK)
       continue;    // go back to start
      // all tests ok
      break;  // exit loop
    } // end while(true)

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Start code again?

    while(true) loops, while functional, are somewhat of a poor code design as they are sometimes difficult to decipher what a section of code is suppose to do. That's not to say never use while(true) loops, as sometimes they are much easier to read than the alternatives (or produce shorter code), but try to use some of these alternatives before resorting to while(true) loops.

    Ex. alternative: Use a boolean flag to determine if you can quit.
    boolean canQuit = false;
    while(!canQuit)
    {
         // <TODO> do test one
         if(test1_OK)
         {
               // <TODO> do test two
              if(test2_OK)
              {
                   // <TODO> do test three
                   if(test3_OK)
                   {
                        // can quit
                        canQuit = true;
                   }
              }
         }
    }

    Here you can easily tell what the code is suppose to do and quickly follow each possible logic path.

    Something else you can do to make your code easier to read (and write/manage) is to create "helper methods". These methods abstract away the details when you want to get the general picture of what some code is doing, and keep your code from getting bloated when you want the same code to be executed multiple times. Something like this:

    boolean canQuit = false;
    while(!canQuit)
    {
         if(doTest1() == true) // doTest1 is a method that you write to perform some stuff and returns true on success and false on fail
         {
              if(doTest2() == true) // doTest2 is a method that you write to perform some stuff... etc. etc.
              {
                   if(doTest3() == true) // doTest3 is a method that you write to perform some stuff... etc. etc.
                   {
                        canQuit = true;
                   }
              }
         }
    }

    then you just need to implement the doTest# methods somewhere and you have easily maintainable code

    public static boolean doTest1()
    {
         System.out.println("did test1 succeed (y/n)?");
         if(new Scanner(System.in).next().equals("y"))
         {
              // test succeeded
              return true;
         }
         else
         {
              // test failed
              return false;
         }
    }

Similar Threads

  1. [SOLVED] selection end and start
    By nasi in forum What's Wrong With My Code?
    Replies: 13
    Last Post: May 10th, 2010, 04:05 AM
  2. Illegal start
    By hing09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 25th, 2010, 05:23 PM
  3. Where to start?
    By tonykasdorf in forum Java Theory & Questions
    Replies: 3
    Last Post: March 4th, 2010, 11:52 PM
  4. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM
  5. Replies: 1
    Last Post: May 7th, 2009, 04:31 AM