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[] args) throws 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

|