| | |
How to go back in "main method"
![]() |
•
•
Join Date: Aug 2008
Posts: 29
Reputation:
Solved Threads: 1
hello, guyzz,,i hope you can help me,,actually,,this is not my whole code.but please help me with this...
public class Calculator {
public static void main (String args[]) {
String exp = JOptionPane.showInputDialog("Enter mathematical expression: ");
exp = infixToPostfix(exp);
JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp);
public static String infixToPostfix (String infix) {
(some codes here).........
else {
JOptionPane.showMessageDialog(null, "Invalid expression.");
System.exit(0);
}
}
if i input an invalid expression, the program automatically read the infixToPostfix method...which prints "Invalid expression"..and i use System.exit(0); because i dont know how to call the 'main method" to ask again a "mathematical expression"....
do you know guys how to go back again in main method without exiting in the program if i input an invalid expression?????i really need your help,,,i would about to pass it tomorrow,,...thank you again guyz..
public class Calculator {
public static void main (String args[]) {
String exp = JOptionPane.showInputDialog("Enter mathematical expression: ");
exp = infixToPostfix(exp);
JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp);
public static String infixToPostfix (String infix) {
(some codes here).........
else {
JOptionPane.showMessageDialog(null, "Invalid expression.");
System.exit(0);
}
}
if i input an invalid expression, the program automatically read the infixToPostfix method...which prints "Invalid expression"..and i use System.exit(0); because i dont know how to call the 'main method" to ask again a "mathematical expression"....
do you know guys how to go back again in main method without exiting in the program if i input an invalid expression?????i really need your help,,,i would about to pass it tomorrow,,...thank you again guyz..
Assuming you something like this:
You cannot return back to main, one the last command is executed the program ends and you don't need the System.exit(0)
try using a while loop that repeats the call to your method and when you enter a specific input exit the while.
Java Syntax (Toggle Plain Text)
public class Calculator { public static void main (String args[]) { String exp = JOptionPane.showInputDialog("Enter mathematical expression: "); exp = infixToPostfix(exp); JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp); } public static String infixToPostfix (String infix) { (some codes here)......... else { JOptionPane.showMessageDialog(null, "Invalid expression."); System.exit(0); } }
You cannot return back to main, one the last command is executed the program ends and you don't need the System.exit(0)
try using a while loop that repeats the call to your method and when you enter a specific input exit the while.
Java Syntax (Toggle Plain Text)
boolean cont = true; while (cont) { String exp = JOptionPane.showInputDialog("Enter mathematical expression: "); if (exp.equels("EXIT")) { cont=false; } else { exp = infixToPostfix(exp); JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp); } }
Check out my New Bike at my Public Profile at the "About Me" tab
you don't really "leave" your main method..
so, instead of focusing how to get back in it, focus on how not to get out of it
as JavaAddict already mentioned, your program will run the code in your main method, and if that is all finished, your program will exit.
assuming that infixToPostfix is not the only method you want to run, you might also consider to place that while loop in your main method
so your main method would not be
run infixToPostfix
end
but rather
check condition
while (condition= true)
run infixToPostfix
run anything else that needs to be ran while the program loops
end
so, instead of focusing how to get back in it, focus on how not to get out of it

as JavaAddict already mentioned, your program will run the code in your main method, and if that is all finished, your program will exit.
assuming that infixToPostfix is not the only method you want to run, you might also consider to place that while loop in your main method
so your main method would not be
run infixToPostfix
end
but rather
check condition
while (condition= true)
run infixToPostfix
run anything else that needs to be ran while the program loops
end
And remove the System.exit(0), it will terminate the program
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Firefox Compatibility help with script (JavaScript / DHTML / AJAX)
- need help with this "method" problem (Java)
- "topantispyware" virus (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: help me
- Next Thread: How to Store two different class objects in Same array. - new to Java ..Need Help
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






