Results 1 to 6 of 6
  1. #1
    Salamander's Avatar
    Salamander is offline Member
    Join Date
    Aug 2010
    Location
    Waxahachie TX USA
    Posts
    8
    Rep Power
    0

    Default [SOLVED] How do I re-start a class?

    I've got a little problem, this I couldn't seem to find a solution for, so I figured I'd post about it.

    I'm still pretty much a beginner so the project is pretty simple, just a multifunction calculator for the command line.

    What I want to do is enable the program to restart itself, if the user tells it to.

    All of the coding in done in one class.

    Anyway here is the bit of code where I have the problem. (Yes I know it's ugly LOL)

    Java Code:
    System.out.println("What Now?");
    System.out.print("\n");
    System.out.println("[1] = Continue");
    System.out.println("[2] = Exit Program");
    System.out.print("\n");
    Scanner DivSelect = new Scanner(System.in);
    int DivChoose = DivSelect.nextInt();
    if(DivChoose == 1)
         {
              System.out.print("\n");
              Calculator ReStart = new Calculator();
              System.out.print("\n");
         }
    else if(DivChoose == 2)
         {
              System.out.print("\n");
              System.out.println("Goodbye");
              System.out.print("\n");
         }
    Last edited by Salamander; 08-04-2010 at 04:31 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your solution appears to be a recursive one: the class creates another instance of itself, and while this could work, I think that most of us would use a loop, such as a while loop, since this is the type of loop you use when you don't know in advance how many times you want it repeated. The while loop would need a test condition, possibly checking a boolean variable that you set depending on the users response to whether or not to restart.

  3. #3
    Salamander's Avatar
    Salamander is offline Member
    Join Date
    Aug 2010
    Location
    Waxahachie TX USA
    Posts
    8
    Rep Power
    0

    Default

    I've thought about using some of the loops, though with the project I'm working on there isn't really a good place to put one.

    I'm considering making another class or using multiple methods.

    Thanks for your help tho. Will change the program a little and see what happens.

    EDIT: Okay with lots of editing I finally solved the problem.

    1. I created a new method called "extra" and added the following loop to the main method.

    Java Code:
     while(true) extra(args);
    Last edited by Salamander; 08-04-2010 at 04:28 AM.

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

    Default

    Salamander, to mark the thread solved no need to edit the post again. Check on the main menu on top of the thread.

  5. #5
    Salamander's Avatar
    Salamander is offline Member
    Join Date
    Aug 2010
    Location
    Waxahachie TX USA
    Posts
    8
    Rep Power
    0

    Default

    Thanks for telling me how to do that. :) This forum system is a tad more advanced than I'm used too, but all the features are admirable.

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

Similar Threads

  1. Replies: 5
    Last Post: 03-20-2009, 10:04 PM
  2. Where to start?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-19-2009, 02:26 PM
  3. How to start
    By sand.softnet1 in forum New To Java
    Replies: 5
    Last Post: 09-05-2008, 07:41 PM
  4. Replies: 9
    Last Post: 08-08-2007, 07:16 PM
  5. How to start
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 08:43 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
  •