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 9 of 9

Thread: One of the most simple loop awnsers

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default One of the most simple loop awnsers

    When I first learned Java, I read that so switch to another class (And say your class is Myclass1 and the other one is Myclass2) You did Myclass2.main(args);

    But you can also apply that same principle to the same class.

    Let's say your in Myclass1, and you need the program to start from the beginning again.

    You can just do Myclass1.main(args);

    In this example I meant for all the classes to start with

    public static void main(String[] args){
    }

    And thats why you put (args) into the Myclass1.main(args);
    If it doesn't have an args in it- well it should work fine.



    Please Reply with any bugs or problems with this theory of looping.
    I know there are a few problems with it, but overall it solves many looping problems.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: One of the most simple loop awnsers

    Hello Firestar912!
    You mean something like that
    public class Test {
        public static void main(String[] args) {      
           //do something
           Test.main(args);     
        }
    }
    The problem with the above code is that the program will never end.

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One of the most simple loop awnsers

    Yea, that would be the point. Because in a something like a guessing game, you never want the game to end.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: One of the most simple loop awnsers

    Quote Originally Posted by Firestar912 View Post
    Yea, that would be the point. Because in a something like a guessing game, you never want the game to end.
    "Never" is relative. You want it to stop at some time. You can keep a variable for that reason and evaluate it in a loop. while and do...while loops can be used such way that the loop keeps going until the variable takes an unwanted value.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: One of the most simple loop awnsers

    It's not a loop, it's recursion. Try inserting a:
    new Exception("Look at my stack!").printStackTrace()
    as the first statement in your main method.

  6. #6
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One of the most simple loop awnsers

    You can make it so when you win the game, it stops looping using for and else.

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: One of the most simple loop awnsers

    Sure, you can do it recursively...but why? What's the advantage? I can tell you a disadvantage: the stack size is increased every time you recurse, and depending upon the size of the application could cause a huge memory footprint and risk a StackOverflowException. There is a reason why loops exist, and why recursion is better suited for navigating things such as data structures. Here's another: java is object oriented - relying on a main method for application workflow is bad practice and reduces the power object oriented programmed provides.
    but overall it solves many looping problems
    What problems? I would encourage you to describe any advantage to what you suggest, because I fail to see any.

  8. #8
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One of the most simple loop awnsers

    Haha, you are only pointing out the bad points. If you are just doing a simple text program- like a guessing game- the code will go away after so much code is generated.

  9. #9
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: One of the most simple loop awnsers

    Quote Originally Posted by Firestar912 View Post
    Haha, you are only pointing out the bad points. If you are just doing a simple text program- like a guessing game- the code will go away after so much code is generated.
    Like I said, I encourage you to point out any advantages - which I have yet to see you do...perhaps because I don't understand what you mean by "the code will go away after so much code is generated".

    I'd say defeating the purpose of writing code in an object oriented manner is a pretty 'bad point'.

Similar Threads

  1. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  2. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 08:13 PM
  3. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 01:58 PM
  4. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 04:31 AM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM

Tags for this Thread