Friday, 14th October 2011
Follow WikiJava on twitter now. @Wikijava

SwitchExample.java

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

This article shows the use of the switch conditional in Java, this may be useful when need to quickly indentify numbers, for example in the case of menu items.

The article show also how to convert a String containing a number into an int value. And shows also how to parse the command line input.

Contents

Parsing the command line parameters

To start a program in java the JVM by default searches for the main method in the class you execute.

The main method has always the following signature:

public static void main(String[] args) {

the parameter String[] args contains all the command line parameters.

For example:

If we create the SwitchExample class, compile it and execute it with:

java SwitchExample.class first 2 third

The array args will contain three String elements. Namely:

["first", "2", "third"]

these can be accessed singularly with:

args[0]
args[1]
args[2]

The easiest block to check if the command line parameters have been inserted properly:

if (args.length != 1) {
			System.err.println("Usage: SwitchExample [1|2|3|4]");
			return;
		}

Converting a String containing a number into an int

int option = Integer.parseInt(args[0]);

The Java API implements for each of the primitive types a correspondent encapsulation Class, in the java.lang package.

Primitive

Type

Size Wrapper Type
char16-bitjava.lang.Character
byte8-bitjava.lang.Byte
short16-bitjava.lang.Short
int32-bitjava.lang.Integer
long64-bitjava.lang.Long
float32-bitjava.lang.Float
double64-bitjava.lang.Double
boolean1-bitjava.lang.Boolean

The classes for the numeric types all contain the method parseXXX(String) that translates a properly formatted String into the primitive type.

Thanks to these Classes you can write:

int option = Integer.parseInt(args[0]);

switch syntax

the syntax of the switch statement is as follows.

switch (key) {
case value:
 
	break;
 
default:
	break;
}

The key is an int variable. The value is an int variable or constant.

remember always to write the break command, to force the exit from the switch, otherwise all the cases will be evaluated, and in the best case also the default option will be executed.

The code

package org.wikijava.basics;
 
public class SwitchExample {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
 
		if (args.length != 1) {
			System.err.println("Usage: SwitchExample [1|2|3|4]");
			return;
		}
 
		int option = Integer.parseInt(args[0]);
 
		switch (option) {
		case 1:
			System.out.println("you typed one");
			break;
		case 2:
			System.out.println("you typed two");
			break;
		case 3:
			System.out.println("you typed three");
			break;
		case 4:
			System.out.println("you typed four");
			break;
		default:
			System.out.println("you typed something unrecognizable");
			break;
		}
 
	}
 
}


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.