main manu

1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division
5 for Choice not in list

Dani AI

Generated

Thread starter posted a very small "main menu" request; correctly asked whether a GUI is wanted and and @sciwizeh asked for clarification. Two practical choices follow: a simple console (keyboard) menu for learning and a minimal Swing dialog-based menu for a quick GUI. Key cautions: validate all numeric input, handle divide-by-zero, and for Swing run UI work on the Event Dispatch Thread (EDT) and avoid long/blocking tasks there.

Console example (simple, easy to step through):

import java.util.Scanner;

public class SimpleMenu {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Choose an operation: 1) Add 2) Subtract 3) Multiply 4) Divide 5) Other 0) Exit");
            String line = sc.nextLine().trim();
            if ("0".equals(line)) break;
            try {
                int opt = Integer.parseInt(line);
                System.out.print("First number: ");
                double a = Double.parseDouble(sc.nextLine());
                System.out.print("Second number: ");
                double b = Double.parseDouble(sc.nextLine());
                switch (opt) {
                    case 1: System.out.println("Result: " + (a + b)); break;
                    case 2: System.out.println("Result: " + (a - b)); break;
                    case 3: System.out.println("Result: " + (a * b)); break;
                    case 4:
                        if (b == 0) System.out.println("Cannot divide by zero.");
                        else System.out.println("Result: " + (a / b));
                        break;
                    default: System.out.println("Option not implemented.");
                }
            } catch (NumberFormatException ex) {
                System.out.println("Invalid input, use digits.");
            }
        }
    }
}

Swing/dialog example (quick GUI using JOptionPane):

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class GuiMenu {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            String[] options = {"Add","Subtract","Multiply","Divide","Other"};
            int sel = JOptionPane.showOptionDialog(null, "Choose operation", "Menu",
                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
            if (sel < 0) return;
            try {
                double a = Double.parseDouble(JOptionPane.showInputDialog("First number:"));
                double b = Double.parseDouble(JOptionPane.showInputDialog("Second number:"));
                if (sel == 3 && b == 0) { JOptionPane.showMessageDialog(null, "Cannot divide by zero"); return; }
                double r = (sel==0? a+b : sel==1? a-b : sel==2? a*b : 0);
                JOptionPane.showMessageDialog(null, "Result: " + r);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Invalid number");
            }
        });
    }
}

For a persistent menubar (not dialogs) use JMenuBar/JMenu/JMenuItem and attach ActionListeners. For learning start with the console version; for a desktop feel use Swing but remember to keep UI code on the EDT and move heavy work to background threads.

Recommended Answers

All 2 Replies

You better provide better info on the request as there is nothing like "option menu". For basic Swing terminology see this

main manu

1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division
5 for Choice not in list

You have posted the same question to a different thread:
http://www.daniweb.com/forums/post661955.html#post661955

When you were asked from sciwizeh to provide more information, why didn't reply, but started a new one?


Any way, please provide with information if it is going to be GUI or keyboard input, so for us to guide you to the right direction.

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.