{"id":187254,"date":"2025-02-04T05:08:22","date_gmt":"2025-02-04T05:08:22","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=187254"},"modified":"2025-02-04T05:08:24","modified_gmt":"2025-02-04T05:08:24","slug":"my-main-java-class-joptionpane-is-not-working","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/04\/my-main-java-class-joptionpane-is-not-working\/","title":{"rendered":"My Main java class JOptionPane is not working."},"content":{"rendered":"\n<p>My Mainjava class JOptionPane is not working.<\/p>\n\n\n\n<p>import javax.swing.JOptionPane;<\/p>\n\n\n\n<p>import java.util.*;<\/p>\n\n\n\n<p>public class Main {<\/p>\n\n\n\n<p>public static void main(String[] args){<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  String dash= (\"----------------------------------\");\n\n  Scanner input= new Scanner(System.in);\n\n     Date date = new Date();\n\n\n\n        System.out.println(date);\n\n        JOptionPane.showInputDialog(null,\n\n                \"Welcome to the Hotel \\n \" +dash  +\n\n                        \" \\n 1. Add a Visitor \\n \"     +\n\n                            \"2. View All Visitors \\n \" +\n\n                            \"3. Delete a Visitor \\n \"     +\n\n                            \"Choose Option Number: \"   );\n\n        int menu= input.nextInt();\n\n        if(menu==1)\n\n        {\n\n        Info in= new Info();\n\n        in.display();\n\n        }\n\n        if(menu==2)\n\n        {\n\n           JOptionPane.showMessageDialog(null, \"Test\");\n\n           \/\/create a new class called view and call the array list and print it\n\n\n\n        }\n\n        if(menu==3)\n\n        {\n\n           Delete del= new Delete();\n\n           del.display();\n\n        }\n\n        else if(menu!=1 | menu!=2 | menu !=3)\n\n        {\n\n           JOptionPane.showMessageDialog(null, \"Doesnt exist\");\n\n        }                             \/\/prints out regardless of number\n\n     }<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>Your <code>Main.java<\/code> class is designed to display a menu using <code>JOptionPane<\/code> and handle user input through the <code>Scanner<\/code> class. However, there are several issues that need to be addressed to ensure the program functions correctly:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>User Input Handling<\/strong>: You&#8217;re using <code>JOptionPane.showInputDialog<\/code> to display the menu and prompt the user for input, but then you&#8217;re using <code>Scanner<\/code> to read the input. This approach is inconsistent. Since <code>JOptionPane.showInputDialog<\/code> returns the user&#8217;s input as a <code>String<\/code>, you should capture this input directly and convert it to an integer for menu selection.<\/li>\n\n\n\n<li><strong>Logical Flow and Error Handling<\/strong>: The current structure of your <code>if<\/code> statements can lead to unintended behavior. Specifically, the <code>else if<\/code> condition <code>else if(menu!=1 | menu!=2 | menu !=3)<\/code> will always evaluate to <code>true<\/code> because a number cannot be simultaneously equal to 1, 2, and 3. This means that any input, including valid ones, will trigger the &#8220;Doesn&#8217;t exist&#8221; message.<\/li>\n\n\n\n<li><strong>Class Definitions<\/strong>: The classes <code>Info<\/code> and <code>Delete<\/code> are referenced but not defined in the provided code. Ensure these classes are properly implemented elsewhere in your project.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s a revised version of your <code>Main.java<\/code> class addressing these issues:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import javax.swing.JOptionPane;\nimport java.util.Date;\n\npublic class Main {\n\n    public static void main(String&#91;] args) {\n        String dash = \"----------------------------------\";\n        Date date = new Date();\n\n        \/\/ Display the current date\n        System.out.println(date);\n\n        \/\/ Display the menu and get user input as a String\n        String input = JOptionPane.showInputDialog(null,\n                \"Welcome to the Hotel \\n\" + dash +\n                        \"\\n 1. Add a Visitor \\n\" +\n                        \"2. View All Visitors \\n\" +\n                        \"3. Delete a Visitor \\n\" +\n                        \"Choose Option Number:\");\n\n        try {\n            \/\/ Convert the input to an integer\n            int menu = Integer.parseInt(input);\n\n            \/\/ Handle menu selection\n            switch (menu) {\n                case 1:\n                    Info in = new Info();\n                    in.display();\n                    break;\n                case 2:\n                    JOptionPane.showMessageDialog(null, \"Test\");\n                    \/\/ Implement the logic to view all visitors\n                    break;\n                case 3:\n                    Delete del = new Delete();\n                    del.display();\n                    break;\n                default:\n                    JOptionPane.showMessageDialog(null, \"Option doesn't exist\");\n                    break;\n            }\n        } catch (NumberFormatException e) {\n            JOptionPane.showMessageDialog(null, \"Invalid input. Please enter a number.\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Consistent User Input Handling<\/strong>: The program now uses <code>JOptionPane.showInputDialog<\/code> to capture user input as a <code>String<\/code> and then attempts to convert this input to an integer using <code>Integer.parseInt(input)<\/code>. This approach eliminates the need for the <code>Scanner<\/code> class and ensures consistency in how user input is handled.<\/li>\n\n\n\n<li><strong>Error Handling<\/strong>: A <code>try-catch<\/code> block is used to catch <code>NumberFormatException<\/code>, which occurs if the user enters non-numeric input. If such an exception is caught, an error message is displayed to the user, prompting them to enter a valid number.<\/li>\n\n\n\n<li><strong>Logical Flow<\/strong>: The <code>switch<\/code> statement replaces the series of <code>if<\/code> statements, providing a clearer and more efficient way to handle menu selections. The <code>default<\/code> case handles any invalid menu options, displaying an appropriate message to the user.<\/li>\n\n\n\n<li><strong>Class Definitions<\/strong>: Ensure that the <code>Info<\/code> and <code>Delete<\/code> classes are properly defined in your project. The <code>display<\/code> methods in these classes should contain the logic for adding and deleting visitors, respectively.<\/li>\n<\/ul>\n\n\n\n<p>By implementing these changes, your program will handle user input more robustly and provide clearer feedback to the user, enhancing the overall user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My Mainjava class JOptionPane is not working. import javax.swing.JOptionPane; import java.util.*; public class Main { public static void main(String[] args){ } The Correct Answer and Explanation is : Your Main.java class is designed to display a menu using JOptionPane and handle user input through the Scanner class. However, there are several issues that need to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-187254","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=187254"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187254\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=187254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=187254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=187254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}