Home Java Beginners Switch Statement example in Java

 
 

Share on Google+Share on Google+

Switch Statement example in Java

Advertisement
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. 

Advertisement

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:");
  try{
  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));
 break;
  case 2:
  System.out.println("Enter the number two=" (x-y));
  break;
  case 3:
  System.out.println("Enetr the number three="(x*y));
  break;
  case 4:
  System.out.println("Enter the number four="(x/y));
  break;
  default:
  System.out.println("Invalid Entry!");
  }
  }
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a numeric value.");
  System.exit(0);
  }

  }
}

Download this Example

Advertisement

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

Discuss: Switch Statement example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:29
snehal
September 21, 2012
java

good site for lerners
biloy
October 10, 2012
Given Example

The code has error.
bala
October 28, 2012
java

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
Hema
July 5, 2013
java

Give some more examples.
kiran
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
nirjharkundu
November 25, 2013
java

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); } } }
DMCA.com