Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:");
    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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

nice program ,to understand switch statement

Posted by krishna on Thursday, 12.27.07 @ 11:06am | #43926

Latest Searches:
show calender page
Web Design Basics Mike
bean:define tags
sort compareTo
3DS MAX Effects Easy s
Linux Caixa Mпâ? ?пâ
XML READER
Ajax Examples
sun jdbc
get windows directory
Create PDF in a Browse
code of image upload i
find char in a string
textbox
books available in bas
Image with mouse drag
get data by using char
insert date with java
varStatus
program to increase si
include a file in dire
character in javascrip
create menubar
JSON AJAX
header footer menu bod
iterator
browse button in html
unicode
insert data to text fi
difference between inc
Insert Data in Table U
core java
Store Procedure in hib
Database MySQL Using S
Error handling in stru
difference between com
Photoshop Effects Shin
determinan
Interview question on
JSF action lestner exa
JSFpanelGridTag
Convert String to Bool
reflection api
3DS MAX Modeling Model
decimal to extended as
JQUERY
how can we select an d
insert coding of autoi
Read a file in java
Combattons la programm
Photoshop Digital Art
java nodequeue tutoria
dynamic insert value J
shell
ajax auto complete
hybernet
Photoshop Drawing Draw
how to install tomcat
Download JDK
add picture to a swing
Flash Interactivity Sc
jsf datatree component
array element
ad
login using JSP
static commands
compare contents of fi
HWPF
j2me servlet
using character to vie
hyperlink in JSF
coding of radio button
actions on menuitems
setpropertyvalue
log4j
PhotoshopTech/Abstract
Photoshop Effects Hole
html:input
PHP Database Related I
richfaces tutorial
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.