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..

Recommended Answers

All 5 Replies

Assuming you something like 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);
  }

}

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.

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);
}
}

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

And remove the System.exit(0), it will terminate the program

I assume you want progrma to act in endless loop;
1 Calculator.java >
2 execute public static void main (String args[])
3 and if error go back to 2 line again, i did it just by typing main(args), exactly what is written after void

Hi kluonius. Welcome to DaniWeb.
Did you notice this thread is 4 years old? It's unlikely the original poster is still waiting for an answer!
ps stultuke's answer above is the right one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.