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 it possible to restart an application?
Page 1 of 19 Replies - 262 Views - Last Post: 28 July 2012 - 01:28 AM
Replies To: Is it possible to restart an application?
#2
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"
}
#3
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.
#4
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.
#5
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;
}
}}}
#6
Re: Is it possible to restart an application?
Posted 27 July 2012 - 06:47 PM
DxnadxC, 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.
#7
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.
#8
Re: Is it possible to restart an application?
Posted 27 July 2012 - 07:46 PM
DxnadxC, 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.
#9
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.
#10
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|