Home Java Beginners Switch Statement example in Java


Share on Google+Share on Google+

Switch Statement example in Java

This is very simple Java program for implementing the switch statement.

Switch Statement example in Java 


This is very simple Java program for implementing the switch statement. Here, you will learn how to use the switch statement in your java program for developing java application. This section provides you the best illustration about some mathematical operations like Addition, Subtraction, Multiplication and division.

Program Overview And Result:

This section also provides you an example with complete java source code for understanding more about the switch statement in Java. The following java program tells you for entering numbers for involving in mathematical operations as your choice (whatever you want to do with both number). Four type of operations are listed through the program in sequence. Whatever you have to perform the operation firstly you have to enter you choice as an existing operation number for selecting operation what you want to do with entered two numbers.

Code Description:

Here we are going to make a program to follows the switch statement. For completion the example firstly we define a class named "SwitchExample" and two integer type values from users. Java I/O Package has a input Stream and a output Stream in which input stream is used for reading the stream and memory allocating . As in this program we are going to create a buffer for the string class that can be used to storing and processing a string character. Here in this program used the pursuing method for two integer value  x and y. Now in this program use once again perform the parseInt method for Enter the choice number. 


After that in this program we are going to use for the continue statement. This program take a number and check weather the number lies between 1 to 5. If the user enter the number 1 then the program print addition and enter the 2 then will be print subtraction so on up to 4 number otherwise when  you user enter after 4 then will print "Invalid Entry" massage using by the println() method. 

Here is the code of this Example

import java.io.*;

public class SwitchExample{
  public static void main(String[] argsthrows Exception{
  int x, y;
  BufferedReader object = new BufferedReader
     (new InputStreamReader(System.in));
  System.out.println("Enter two numbers for operation:");
  x = Integer.parseInt(object.readLine());
  y = Integer.parseInt(object.readLine());
  System.out.println("1. Add");
  System.out.println("2. Subtract");
  System.out.println("3. Multiply");
  System.out.println("4. Divide");
  System.out.println("enter your choice:");
  int a= Integer.parseInt(object.readLine());
  switch (a){  
  case 1:
 System.out.println("Enter the number one=" (x+y));
  case 2:
  System.out.println("Enter the number two=" (x-y));
  case 3:
  System.out.println("Enetr the number three="(x*y));
  case 4:
  System.out.println("Enter the number four="(x/y));
  System.out.println("Invalid Entry!");
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a numeric value.");


Download this Example


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2007

Related Tutorials

Ask Questions?    Discuss: Switch Statement example in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 1, 2011

how to use switch java
Ian Quidato
September 6, 2011

Your program is quite understandable but I think it is better to put also its flowchart. Can you please give me the flowchart of the program because I am planning to have it as my project program. thanks.
September 8, 2011

give some more example
September 14, 2011

i wnat to learn more and explore,about programs & codes,but i cant,how?/hahaha
October 4, 2011
Switch statement

The switch statement above forms an alternative for the dangling else statement in the famous IF statement.But I think it is the best alternative for giving options or creating menus in substitute of the dangling else statement,just to eliminate many errors that can result from using the IF statement:more so errors during compilation.
mebrahtu hafte
November 3, 2011

write a java statement that display the services used in cafe using switch case
December 14, 2011
java programming

i want how to use java statments java full packages
Jeff Richard A. Arellano
January 22, 2013
syntax error

System.out.println("Enetr the number three="+ (x*y)); break; "ENETR"
January 10, 2012
cod (Scanner and case)

System.out.println("1 enter the file name"); System.out.println("2 Display the text"); System.out.println("3 Search within the text"); System.out.println("4 Exit");
January 23, 2012

Excellent !!!
March 9, 2012

Bad explanation. Proof-reading is always a good thing. The example was way too complicated to simply show how a switch functions.
wasim akram
March 23, 2012
hi i want to know more about you...

nice website, i read the topics from this site in exams.. very good job thanks..
March 25, 2012

Actually I am visiting this site first time and I am very impressed.Thats why I am leaving this comment......
sumit badaya
May 24, 2012

hello sir, grateful for helps in java by you. Wanted to know why have you used object.readLine() whats the purpose ? can i skip it in my program?
sumit badaya
May 24, 2012

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at MiniCalc.main(MiniCalc.java:9) i got this error in my program which is as follows..i dont want to import anything. ___ class MiniCalc { public static void main(String args[]) { int x,y; x=Integer.parseInt (args[0]); y=Integer.parseInt (args[1]); int a=Integer.parseInt (args[2]); switch (a) { case 1: System.out.println("Enter the number one=" +(x+y)); break; case 2: System.out.println("Enter the number one=" +(x-y)); break; case 3: System.out.println("Enter the number one=" +(x*y)); break; case 4: System.out.println("Enter the number one=" +(x/y)); break; default: System.out.println("Invalid Entry!"); } } } _____
Raza ullah burki
June 30, 2012
java progrm error

import javac.swing.*: class TestSwitch { public static void main(String str[]) { int a,b,c; char ch,s; s=JOptionPane.ShowInputDialog("enter first value"); a=Integer.parseInt(s); s=JOptionPaneshowInputDialog("enter second value"); b=Integer.parseInt(s); ch=JOptionPane.showInputDialog("enter a symbol"); switch(ch) { case"+"; c=a+b; JOptionPane.showInputDialog(null,"addition="+c); break; case"-" c=a-b; JOptionPane.showInputDialog(null,"subtraction="+c); break; case"*"; c=a*b; JOptionPane.showInputDialog(null,"addition="+c); default: JOptionPane.showInputDialog(null,"invalud operator="+c); } System.exit(0); } }
July 11, 2012
what is denoted by if there is ==> switch (x%7)

i'd be v thankful to your answer
Matib Nicole
August 4, 2012
Java Program Examples

Very Helpful Example is the best way to teach.
Hitesh Khatri
December 17, 2012
For java program

import.java.io.*; class NoOfDays { public static void main(String args[]) { Console con = System.console(); int month, year, numdays = 0; //int month = 2; //int year = 2000; //int numdays = 0; System.out.println("Enter the month"); month = Integer.parseInt(con.readLine()); System.out.println("Enter the year"); year = Integer.parseInt(con.readLine()); switch(month) { case 0: case 1: case 4: case 6: case 7: case 9: case 11: numdays = 31; break; case 3: case 5: case 8: case 10: numdays = 30; break; case 2: if( ((year % 4 == 0 ) && !(year % 100 ==0)) || (year %400 == 0) ) numdays = 29; else numdays = 28; break; default: System.out.println("Invalid Month"); } System.out.println("Number od days" +numdays); } } this program is giving a error identifier Expected in line 1(import.java.io.*;) Please help me :(
September 12, 2012

good views
September 21, 2012

good site for lerners
October 10, 2012
Given Example

The code has error.
October 28, 2012

java is my fav sub
Jimmie Pierre
December 3, 2012
Java program

Create a program that gives you the following options for a dental office: 1. Cleaning - $35.00 2. Extractions - $70.00 3. X-rays - $150.00 4. Quit when the user selects an option, display the total cost that falls under that category.
Niraj Chaurasia
May 1, 2013
Java Program

I Like Java Program
July 5, 2013

Give some more examples.
August 4, 2013
String in switch

How we can pass the String in switch case? please help me
Kuldeep Singh
October 1, 2013
Comment on SwitchExample

Thanks for writing this program. I had a doubt. But now, it is cleared because of this program. http://www.roseindia.net/java/beginners/SwitchExample.shtml
November 25, 2013

import java.io.*; import java.util.Scanner; public class swcase1{ public static void main(String[] args) throws Exception{ BufferedReader object = new BufferedReader (new InputStreamReader(System.in)); System.out.println("1. Fibonacci"); System.out.println("2. prime"); System.out.println("enter your choice:"); int a= Integer.parseInt(object.readLine()); switch (a) { case 1: { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int entered_num= input.nextInt(); System.out.println("The Fibonacci Sequence of the Number " + entered_num + " is:"); int constant1 = 1; int constant2= 1; int nextNumber = 0; int count=0; for (int i=0;i<=entered_num;i++) { System.out.println("F" + count + "=" + nextNumber); constant1 = constant2; constant2 = nextNumber; nextNumber = constant1 + constant2; count++; } } break; case 2: { int n, status = 1, num = 3; Scanner in = new Scanner(System.in); System.out.println("Enter the number of prime numbers you want"); n = in.nextInt(); if (n >= 1) { System.out.println("First "+n+" prime numbers are :-"); System.out.println(2); } for ( int count = 2 ; count <=n ; ) { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; } } if ( status != 0 ) { System.out.println(num); count++; } status = 1; num++; } } break; default: System.out.println("Invalid Entry!"); } { System.exit(0); } } }