9 Replies - 262 Views - Last Post: 28 July 2012 - 01:28 AM Rate Topic: -----

#1 DxnadxC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Is it possible to restart an application?

Posted 27 July 2012 - 04:57 PM

Hello, I wanted to know if it was possible to restart an application with code. I want to give the user an option to do something again or not. If it is possible, what is the code and where do I insert it?
Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to restart an application?

#2 burakaltr  Icon User is offline

  • D.I.C Head

Reputation: 86
  • View blog
  • Posts: 212
  • Joined: 07-November 10

Re: Is it possible to restart an application?

Posted 27 July 2012 - 05:05 PM

Use

while(true){


//your whole execution block goes here


//In the end, ask if user the wants to exit. If so, break "while"
}

Was This Post Helpful? 2
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Is it possible to restart an application?

Posted 27 July 2012 - 05:13 PM

Going with what burakaltr said, "your whole execution block", or the process you want repeated, warrants its own method. If something needs to be done more than once, you don't want to write the code it performs more than once. If you put it in a method, and use loops, you can do what you want any number of times, with very little code.
Was This Post Helpful? 1
  • +
  • -

#4 DxnadxC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 05:24 PM

Ok I tried that but now typing yes terminates it as well.
Was This Post Helpful? 0
  • +
  • -

#5 DxnadxC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 06:43 PM

This is the code I'm using. It is a program to multiply 2 numbers.
import java.util.Scanner;

public class Multiplication {
	public static void main(String[] args){

		
		while(true){
		

		System.out.println("Type your first number.");

		Scanner sc = new Scanner(System.in);

		String st = sc.next();
		
		int i = sc.nextInt();
		
		System.out.println("Type your second number.");

		int y = sc.nextInt();
		
		for(int j=0; j<=0; j++);

		System.out.println("The answer is...  " + (i * y));
		
		System.out.println("\nWould you like to do another equation?");
		{
			
		Scanner start = new Scanner(System.in);
		String one = start.next();
		
		if(one.equalsIgnoreCase("yes"));
	
		
		if(one.equalsIgnoreCase("no"));
		break;
		
	
		
		}
		
		}}}

Was This Post Helpful? 0
  • +
  • -

#6 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 06:47 PM

View PostDxnadxC, on 27 July 2012 - 06:43 PM, said:

This is the code I'm using. It is a program to multiply 2 numbers.
import java.util.Scanner;

public class Multiplication {
	public static void main(String[] args){

		
		while(true){
		

		System.out.println("Type your first number.");

		Scanner sc = new Scanner(System.in);

		String st = sc.next();
		
		int i = sc.nextInt();
		
		System.out.println("Type your second number.");

		int y = sc.nextInt();
		
		for(int j=0; j<=0; j++);

		System.out.println("The answer is...  " + (i * y));
		
		System.out.println("\nWould you like to do another equation?");
		{
			
		Scanner start = new Scanner(System.in);
		String one = start.next();
		
		if(one.equalsIgnoreCase("yes"));
	
		
		if(one.equalsIgnoreCase("no"));
		break;
		
	
		
		}
		
		}}}


You have a semicolon after your if statement. You want something like

if(one.equalsIgnoreCase("no"))
break;

Otherwise the if(one.equalsIgnoreCase("no")); is it's own statement and does nothing. Then break; is a separate statement and should break your loop every time.

Also I personally would delete the if(one.equalsIgnoreCase("yes")); because it doesn't add anything to your code.
Was This Post Helpful? 1
  • +
  • -

#7 DxnadxC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 07:30 PM

Thanks it now works except that you must type the first number twice. Could you tell me where and how to make it so you must only type it once. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#8 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 07:46 PM

View PostDxnadxC, on 27 July 2012 - 07:30 PM, said:

Thanks it now works except that you must type the first number twice. Could you tell me where and how to make it so you must only type it once. Thanks in advance.


Look at your code for a moment where you entered int i = sc.nextInt();

Do you notice something directly above it that could cause that?

I'd suggest reading up a bit on control flow and objects so you understand them better. :)
Was This Post Helpful? 1
  • +
  • -

#9 DxnadxC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Re: Is it possible to restart an application?

Posted 27 July 2012 - 09:58 PM

I took out the String and it all works fine now. Thanks to all of you who helped.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Is it possible to restart an application?

Posted 28 July 2012 - 01:28 AM

No need to create the Scanner any more than once
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1