here's my code below and i cant seem to find out how to return to public static void main after selecting yes in the dialog box. this cud be a very damn question however i get confuse about how to resolve this. thanks. 
by the way, this program is still incomplete. iam just stuck with the dialog box to return to main (); Hope you could assist,thanks.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midterm_exercise_2;

import java.util.Iterator;
import java.util.List;
import javax.swing.*;

public class Main {

    public static void
            main(String[] args) {

         int m = Integer.parseInt(JOptionPane.showInputDialog("--MENU--\n\n"
                 + "[1] PreEmptive Scheduling Algorithm\n"
                 + "[2] Non-PreEmptive Scheduling Algorithm\n"
                 + "\nEnter Choice:"));
               
         if (m ==1){
             PreEmpt();
             YesNo();
         }
         else
             NonPreEmpt();



    }


  public static void YesNo(){

        System.out.printf("JOptionPane.YES_OPTION    = %d%n" +
                          "JOptionPane.NO_OPTION     = %d%n" +
                          "JOptionPane.CLOSED_OPTION = %d%n",
                           JOptionPane.YES_OPTION,
                           JOptionPane.NO_OPTION,
                           JOptionPane.CLOSED_OPTION);
        int more = JOptionPane.YES_OPTION;
        do {
            more = JOptionPane.showConfirmDialog(null, "Do you want to go back to Main Menu?", "Confirmation",
                                      JOptionPane.YES_NO_OPTION);
            System.out.println("more = " + Main ());
        } while(more == JOptionPane.YES_OPTION);

        
  }

  public static void PreEmpt() {

        int m = Integer.parseInt(JOptionPane.showInputDialog("--MENU--\n\n"
                + "[1] First Come - First Served\n"
                + "[2] Shortest Job First\n"
                + "[3] Priority Scheduling\n"
                + "Enter Choice:"));


        if (m == 1) {
            FCFS();
        } else if (m == 2) {
            SJF();
        } else if (m == 3) {
            P();
        } else {
            JOptionPane.showMessageDialog(null, "Error Message", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }


  public static void NonPreEmpt() {

        int m = Integer.parseInt(JOptionPane.showInputDialog("--MENU--\n\n"
                + "[1] First Come - First Served\n"
                + "[2] Shortest Job First\n"
                + "[3] Priority Scheduling\n"
                + "Enter Choice:"));


        if (m == 1) {
            FCFS();
        } else if (m == 2) {
            SJF();
        } else if (m == 3) {
            P();
        } else {
            JOptionPane.showMessageDialog(null, "Error Message", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

//FIRST COME FIRST SERVED SCHEDULING
    public static void FCFS() {

        int bt[] = new int[4], wt[] = new int[4], twt = 0, awt, num;
        String output1[] = new String[10];

        for (num = 0; num <= 3; num++) {
            bt[num] = Integer.parseInt(JOptionPane.showInputDialog("\nEnter Burst time for P" + (num + 1) + " : "));
        }

        for (num = 0; num <= 3; num++) {
            if (num == 0) {
                wt[num] = 0;
            } else {
                wt[num] = wt[num - 1] + bt[num - 1];
                output1[num] = "\nWaiting time for P" + (num + 1) + " = " + wt[num];
            }
        }

        for (num = 0; num <= 3; num++) {
            twt += wt[num];
        }
        awt = twt / 4;

        JOptionPane.showMessageDialog(null, output1, "FCFS", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null, "\nAverage Waiting Time =" + awt, "FCFS", JOptionPane.INFORMATION_MESSAGE);
    
        
    }

//SHOTEST JOB FIRST
    public static void SJF() {

        int bt[] = new int[4], wt[] = new int[4], twt = 0, awt, x, num, temp = 0, p[] = new int[4];
        boolean found = false;
        String output1[] = new String[10];

        for (num = 0; num <= 3; num++) {
            bt[num] = Integer.parseInt(JOptionPane.showInputDialog("\nEnter Burst time for P" + (num + 1) + " : "));
        }

        for (num = 0; num <= 3; num++) {
            p[num] = bt[num];
        }

        for (x = 0; x <= 2; x++) {
            for (num = 0; num <= 2; num++) {
                if (p[num] > p[num + 1]) {
                    temp = p[num];
                    p[num] = p[num + 1];
                    p[num + 1] = temp;
                }
            }
        }

        for (num = 0; num <= 3; num++) {
            if (num == 0) {
                for (x = 0; x <= 3; x++) {
                    if (p[num] == bt[x] && found == false) {
                        wt[num] = 0;
                        output1[num] = "\nWaiting time for P" + (num + 1) + " = " + wt[num];
                    }
                }
            } else {
                for (x = 0; x <= 3; x++) {
                    if (p[num] == bt[x] && found == false) {
                        wt[num] = wt[num - 1] + p[num - 1];
                        output1[num] = "\nWaiting time for P" + (num + 1) + " = " + wt[num];
                    }
                }
            }
        }

        for (num = 0; num <= 3; num++) {
            twt += wt[num];
        }

        awt = twt / 4;

        JOptionPane.showMessageDialog(null, output1, "SJF", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null, "\n\nAverage waiting time: " + awt, "SJF", JOptionPane.INFORMATION_MESSAGE);
    }

//Priority Scheduling
    public static void P() {

        int bp[] = new int[4], wtp[] = new int[5], p[] = new int[4], sp[] = new int[4], twt = 0, awt, num, x, temp = 0;
        boolean found = false;

        String output1[]=new String[10];

        for (num = 0; num <= 3; num++) {
            bp[num] = Integer.parseInt(JOptionPane.showInputDialog("\nEnter Burst time for P" + (num + 1) + " : "));

        }

        for (num = 0; num <= 3; num++) {
            p[num] = Integer.parseInt(JOptionPane.showInputDialog("\nEnter Priority for P" + (num + 1) + " : "));

        }

        for (num = 0; num <= 3; num++) {
            sp[num] = p[num];
        }

        for (x = 0; x <= 2; x++) {
            for (num = 0; num <= 2; num++) {
                if (sp[num] > sp[num + 1]) {
                    temp = sp[num];
                    sp[num] = sp[num + 1];
                    sp[num + 1] = temp;
                }
            }
        }

        for (num = 0; num <= 3; num++) {
            if (num == 0) {
                for (x = 0; x <= 3; x++) {
                    if (sp[num] == p[x] && found == false) {
                        wtp[num] = 0;
                        output1[num] = "\nWaiting time for P" + (x + 1) + " = " + wtp[num];
                        temp=x;
                        p[x]=0;
                        found = true;
                    }
                }
                found = false;
            } else {
                for (x = 0; x <= 3; x++) {
                    if (sp[num] == p[x] && found == false) {
                        wtp[num] = wtp[num - 1] + bp[temp];
                        output1[num] = "\nWaiting time for P" + (x + 1) + " = " + wtp[num];
                        temp = x;
                        p[x] = 0;
                        found = true;
                    }
                }
                found = false;
            }
        }

        for (num = 0; num <= 3; num++) {
            twt = twt + wtp[num];
        }

        JOptionPane.showMessageDialog(null, output1, "Priority", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null, "\n\nAverage waiting time: " + (awt = twt / 5), "Priority", JOptionPane.INFORMATION_MESSAGE);
    }

   

}

Dani AI

Generated

Brief diagnosis of ’s symptom: the current YesNo() both tries to call Main() inside a println (invalid) and uses a do/while that keeps re-showing the confirm dialog while the user answers YES — so it never hands control back to a menu. Rather than trying to call main() from inside the dialog method, restructure so the main menu runs in a loop and the confirm dialog simply returns a boolean.

This follows the advice from (keep menus in methods and loop) and (return values instead of void). Example pattern (JOptionPane style) that avoids recursion and is easy to reason about:

public static boolean backToMain() {
    int r = JOptionPane.showConfirmDialog(null,
        "Return to main menu?", "Confirm", JOptionPane.YES_NO_OPTION);
    return r == JOptionPane.YES_OPTION;
}

public static void main(String[] args) {
    while (true) {
        String s = JOptionPane.showInputDialog(
            "--MAIN MENU--\n[1] PreEmptive\n[2] Non-PreEmptive\n[3] Exit\nEnter choice:");
        if (s == null) break; // user cancelled
        int choice;
        try { choice = Integer.parseInt(s); } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid input");
            continue;
        }
        switch (choice) {
            case 1: PreEmpt(); break;
            case 2: NonPreEmpt(); break;
            case 3: return;
            default: JOptionPane.showMessageDialog(null, "Invalid option"); continue;
        }
        if (!backToMain()) break;
    }
}

Notes and troubleshooting: validate input (null from cancel and NumberFormatException), avoid calling main() to “restart” (it makes flow and state harder to follow), and prefer small helper methods that return booleans or ints so callers decide what to do next. Replacing the current YesNo() with a simple boolean-return helper (or moving the confirm into main) will make the control flow clear and fix the behavior.

Recommended Answers

All 4 Replies

Here is an example of how to create a simple menu. You can put that in a method and call it. You can have different menus in different methods. And you can have those methods called from another menu-method:

void menu_1 () {

int choice = 0;
while (true) {
  // print the menu
  //get the input
  
  choice = ...;

  if (choice==1) {
    // do something
  } else if (choice==2) {
    //
  } .... else if (choice==4) {
    // exit the loop
    break;
  } else {
     System.out.println("Invalid choice");
  } 
}

}

You should make the necessary changes for your case.

And you can have 1 method with the main menu: PreEmptive / Non-PreEmptive

And 2 others with menus for PreEmptive and Non-PreEmptive

In the method with the main menu, at the ifs with the choices you can call the other menus:

if (choice==1) {
    // call menu of PreEmptive (different method with its own menu)
  } else if (choice==2) {
    // call menu of Non - PreEmptive (different method with its own menu)
  } .... else if (choice==3) {
    // exit the loop
    break;
  } else {
     System.out.println("Invalid choice");
  }

If you want to get some return values then you need to change "void" in methods you calling for whatever return type you need boolean/int/string/etc.

you been 1 second faster LOL

got it tnx ...(*-*)...

tnx too peter_budo :):)

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.