• Skip to main content
  • Skip to primary sidebar

Founder at work

Master the skills to create and launch your next SaaS company

  • About Me
  • Reviews
  • Resources
  • Community
  • Contact
  • Lessons
  • Series
  • Search
  • Login

How to use switch Statement in Java?

September 27, 2012 by Rick Hernandez Leave a Comment

import javax.swing.JOptionPane;
class cambien
{
  public static void main (String [] args)
  {
    String response;
    response = JOptionPane.showInputDialog
   ("what is your age?");
    if (response == null)
    {
      JOptionPane.showMessageDialog
        (null, "You clicked on the cancel button");
      System.exit (0);
    } //end of if satement
    
    else
      if (response.equals(""))
    {
    JOptionPane.showMessageDialog
      (null, " You must make an entry in the inputbox");
    System.exit (0);
    } // end of else if
    
    if(Integer.parseInt(response) > 25)
    {
      JOptionPane.showMessageDialog
        (null, response + " You must be Graduated");
      System.exit (0);
    }// end of if satement
    
    switch (Integer.parseInt(response)) 
    {
    case 22:
      JOptionPane.showMessageDialog
      (null, response + " You have 1 year untill Graduation");
      break;
    case 21:
      JOptionPane.showMessageDialog
      (null, response + " You have 2 years untill Graduation");
      break;
    case 20:
      JOptionPane.showMessageDialog
      (null, response + " You have 3 years untill Graduation");
      break; // end of case statements
    
    default:
      JOptionPane.showMessageDialog
    (null, response + " You have a long time untill you Graduate"); 
    }// end of switch stememnts
    System.exit (0);
    
  }// end of void main
}// end of cambien class


Rick Hernandez

I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Java, Programming

Primary Sidebar

Learn How To Take Your Idea And Convert It Into An App From Scratch Even If You Have Never Written A Single Line Of Code.

  • How to Find Your Next Great App Idea
  • The Secret To Getting PAID To Build Your Own App
  • How To Create Your App (SaaS, Mobile, VR, AR, Game)
  • How To Monetize Your Apps
Learn More
4.82 Ratings

Copyright © 2025 · JSecademy Blog Theme on Genesis Framework · WordPress · Log in