How to go back in "main method"

Reply

Join Date: Aug 2008
Posts: 29
Reputation: letlet_pogs has a little shameless behaviour in the past 
Solved Threads: 1
letlet_pogs letlet_pogs is offline Offline
Light Poster

How to go back in "main method"

 
0
  #1
Sep 25th, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,672
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 225
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How to go back in "main method"

 
0
  #2
Sep 25th, 2008
Assuming you something like this:

  1. public class Calculator {
  2.  
  3. public static void main (String args[]) {
  4. String exp = JOptionPane.showInputDialog("Enter mathematical expression: ");
  5. exp = infixToPostfix(exp);
  6. JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp);
  7. }
  8.  
  9. public static String infixToPostfix (String infix) {
  10.  
  11. (some codes here).........
  12.  
  13. else {
  14. JOptionPane.showMessageDialog(null, "Invalid expression.");
  15. System.exit(0);
  16. }
  17.  
  18. }

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.

  1. boolean cont = true;
  2. while (cont) {
  3. String exp = JOptionPane.showInputDialog("Enter mathematical expression: ");
  4. if (exp.equels("EXIT")) {
  5. cont=false;
  6. } else {
  7. exp = infixToPostfix(exp);
  8. JOptionPane.showMessageDialog(null,"Postfix expression: " +"\n"+ exp);
  9. }
  10. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to go back in "main method"

 
0
  #3
Sep 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,672
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 225
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How to go back in "main method"

 
0
  #4
Sep 25th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC