How do you restart a method currently being executed?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 5087 Views - Last Post: 18 March 2012 - 08:16 PM Rate Topic: -----

#1 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

How do you restart a method currently being executed?

Posted 18 March 2012 - 12:36 PM

Hi, I was wondering if it was possible to restart a method that is currently being executed. For example, my program does this:
public static void main(String[] args)
{
   Takes 5 arguments on the command line (args0,..., args4).
   Executes a method called startGame with those args as 
   parameters.
}


public static void startGame(int args0,..., int args4)
{
   Asks the person to press a key and the program does different things 
   depending on what key is pressed.
   If he presses 'p', the game must restart, 
   which means that this current method must 
   stop and restart with the same parameters.
}


This is an ongoing problem for me. Whenever I have a problem in Java, it's always because a method cannot return more than one thing. I wanted to do something like:

if (keypressed.equalsIgnoreCase("p"))
return; // To stop the current execution of 
      this method


But then what? It's not gonna do anything after that. I wanna go back to my main method and tell it to keep executing the startGame method, but I really don't know if that's possible.

This post has been edited by jon.kiparsky: 18 March 2012 - 12:53 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How do you restart a method currently being executed?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 12:44 PM

public static void main(String[] args) {
    do {
       char userInput = myMethod(args[0], args[1], 
    } while(userInput == 'p');
}


Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1405
  • View blog
  • Posts: 3,120
  • Joined: 05-April 11

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 12:48 PM

So if the user makes some invalid inputs, then you want the startGame to be called all over again?

You could do that by using a loop

public class NewGame {

	public static void main(String[] args) {
		new NewGame(args);
	}
	
	public NewGame(String[] args) {
		System.out.println("Starting game");
		
		while (!startGame(args[0]));
		
		System.out.println("The game is started!");
	}
	
	public boolean startGame(String something) {
		if (!something.equals("something"))
			return false;
		return true;
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7869
  • View blog
  • Posts: 13,347
  • Joined: 19-March 11

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 01:02 PM

A while loop is the simplest approach. There are others. A possibly more robust approach would be to provide a particular Game with a set of parameters, and then call an initialize() method, which would set up the game and pass control to whatever Controller you're using (ie, wait for input or start animations and accept player interaction, or whatever). You're going to accept various keyboard input - one of the inputs you trap is 'p' which calls the initialize method again, and starts you over. This might be more robust because it's possible you might want to allow the player to change parameters while in the game, and this would let you preserve those changes. (you might have another key reset with the original parameters, if you hang on to them).



 while (!startGame(args[0]));


can't find that symbol, CasiOo! :)
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1405
  • View blog
  • Posts: 3,120
  • Joined: 05-April 11

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 02:15 PM

View Postjon.kiparsky, on 18 March 2012 - 01:02 PM, said:

A while loop is the simplest approach. There are others. A possibly more robust approach would be to provide a particular Game with a set of parameters, and then call an initialize() method, which would set up the game and pass control to whatever Controller you're using (ie, wait for input or start animations and accept player interaction, or whatever). You're going to accept various keyboard input - one of the inputs you trap is 'p' which calls the initialize method again, and starts you over. This might be more robust because it's possible you might want to allow the player to change parameters while in the game, and this would let you preserve those changes. (you might have another key reset with the original parameters, if you hang on to them).



 while (!startGame(args[0]));


can't find that symbol, CasiOo! :)


Seems to find all symbols here ;) what symbol can't you find?
Was This Post Helpful? 0
  • +
  • -

#6 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 02:27 PM

Ignore this post.

This post has been edited by Newtype: 18 March 2012 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 02:33 PM

Oops, please ignore the previous post. I accidentally hit a button and it posted and I can't find the edit button anywhere.
I did something and tested it and it works. However, I don't know if the Java gurus would be angry if they saw this. Is it something that is acceptable?

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

public static void startGame(args)
{
   String keypressed;
   keypressed = scan.next();
   if (keypressed.equalsIgnoreCase("p")
   {
      startGame(args);
      return;
   }
}


This post has been edited by Newtype: 18 March 2012 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 02:44 PM

It is horrible :)
If you fall on freak who loves your game and play it 2 weeks in a row hitting the "p" key a few millions times you will have a stack overflow
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7869
  • View blog
  • Posts: 13,347
  • Joined: 19-March 11

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 02:53 PM

View PostNewtype, on 18 March 2012 - 04:33 PM, said:

Oops, please ignore the previous post. I accidentally hit a button and it posted and I can't find the edit button anywhere.
I did something and tested it and it works. However, I don't know if the Java gurus would be angry if they saw this. Is it something that is acceptable?

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

public static void startGame(args)
{
   String keypressed;
   keypressed = scan.next();
   if (keypressed.equalsIgnoreCase("p")
   {
      startGame(args);
      return;
   }
}




This is inadvertant recursion. As pbl says, it's going to cause a problem if you do it a few million times, otherwise it's not a performance issue. However, it might be better to have startGame return a boolean value. If you return "true" then run startGame() again, otherwise, terminate the program.
Was This Post Helpful? 2
  • +
  • -

#10 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 04:18 PM

View Postjon.kiparsky, on 18 March 2012 - 04:53 PM, said:

However, it might be better to have startGame return a boolean value. If you return "true" then run startGame() again, otherwise, terminate the program.


I tried this but it doesn't work because if the user presses 'p', it will quit the startGame method and return true. It will then execute startGame again, but once it finishes, startGame will start again because it needs to check the boolean value of the loop.
Was This Post Helpful? 0
  • +
  • -

#11 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 05:45 PM

All right, I think I got it, unless someone says that this is illegal too.

The first game starts from the main method, but the other games don't have to. I just put a while loop at the beginning of the startGame method and I have a boolean which at the end of the method tells me if the loop must run. Since everything is in a second while, when the person presses 'p', it breaks from the while and goes straight to the end of the first while, where the loop is executed.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 06:10 PM

What is wrong with may post #2
You method just have to return as a char the last character entered "p" to start all over again any other one to exit the game
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7869
  • View blog
  • Posts: 13,347
  • Joined: 19-March 11

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 07:08 PM

View PostNewtype, on 18 March 2012 - 06:18 PM, said:

View Postjon.kiparsky, on 18 March 2012 - 04:53 PM, said:

However, it might be better to have startGame return a boolean value. If you return "true" then run startGame() again, otherwise, terminate the program.


I tried this but it doesn't work because if the user presses 'p', it will quit the startGame method and return true. It will then execute startGame again, but once it finishes, startGame will start again because it needs to check the boolean value of the loop.



while (startGame(params)) ;




like so:

[jon: Sun Mar 18 22:06]
/Users/jon/java/junk:523 $ cat Junk.java
import java.util.Scanner;

public class Junk

{
	public static void main(String [] args)


	{
		while(startGame());
	
		System.out.println("Thanks for playing!");
	}


	public static boolean startGame()
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("Starting new game...\nPress 'p' to play again");
		return (scan.nextLine().charAt(0)=='p');
	}
}

[jon: Sun Mar 18 22:06]
/Users/jon/java/junk:524 $ javac Junk.java
[jon: Sun Mar 18 22:06]
/Users/jon/java/junk:525 $ java Junk
Starting new game...
Press 'p' to play again
p
Starting new game...
Press 'p' to play again
p
Starting new game...
Press 'p' to play again
p
Starting new game...
Press 'p' to play again
p
Starting new game...
Press 'p' to play again
q
Thanks for playing!
[jon: Sun Mar 18 22:07]
/Users/jon/java/junk:526 $
Was This Post Helpful? 1
  • +
  • -

#14 Newtype  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 12

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 07:10 PM

View Postpbl, on 18 March 2012 - 08:10 PM, said:

What is wrong with may post #2
You method just have to return as a char the last character entered "p" to start all over again any other one to exit the game


If I understand correctly, it asks the user to enter the character in the main method. It can't, it must ask the user while he's playing, when the startGame method is being executed. Suppose he's playing the game but doesn't know how to find the solution and made too many mistakes, if he wants to start over, he can press 'p' and a new game will generate. I don't think the main method can detect that the user pressed 'p' while the startGame method was being executed, at least not with what I've learned so far.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: How do you restart a method currently being executed?

Posted 18 March 2012 - 07:11 PM

Jon you change startGame() from char (my initial post) to boolean
But why not ? :) Both way are simple to implement
Acually your's is better it is startGame() that determine the exit/restart condition not main() :^:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2