CoreJava Programs Source codes | Examples | Video Tutorials Download

Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Monday, February 16, 2009

StringTokenizer Program


/**

* Write a example for StringTokenizer.

*


*

*/

public class StringTokenizer {

public void stringTokenizerDemo() {

// source string

String strSource = "Ritesh Loves India";

// delimeter string

String strDeli = "i";

System.out.println("\nSource String is : " + strSource);

System.out.println("\nExample 1:-");

System.out.println("------------");

System.out.println("\tDelimeter String is : " + strDeli);

System.out.println("\tTokens Are :");

// specify the string with delimeter string by which it would be splited

java.util.StringTokenizer st = new java.util.StringTokenizer(strSource, strDeli);

while (st.hasMoreTokens()) {

System.out.println("\t\t" + st.nextToken());

}

// another example

strDeli = " ";

System.out.println("Example 2:-");

System.out.println("------------");

System.out.println("\tDelimeter String is : " + strDeli + "");

System.out.println("\tTokens Are : ");

st = new java.util.StringTokenizer(strSource, strDeli);

while (st.hasMoreTokens())

System.out.println("\t\t" + st.nextToken());

}

/**

* execution begin from here

*

* @param args

* command line argument

*/

public static void main(String[] args) {

new StringTokenizer().stringTokenizerDemo();

}

}

Program to lists all the constructors, methods and fields of class

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

/**

* Make a class which lists all the constructors, methods and fields of the

* specified class.

*

*

*/

public class ClassPropertyTest1 {

/**

* execution begins from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

// get the data of MyClass

new ClassPropertyTest1().getSourceClassData();

}

/**

* gets all the data of class SourceClass

*

*/

public void getSourceClassData() {

System.out.println("SourceClass Details : ");

System.out.println("---------------------");

SourceClass srcObj = new SourceClass();

System.out.println("\tAvailable Fields Are ::");

System.out.println("\t-----------------------");

int i = 0;

for (Field fldObj : srcObj.getClass().getDeclaredFields()) {

System.out.println("\tField (" + (i++) + ") : " + fldObj);

}

System.out.println("\n\n\tAvailable Constructors Are ::");

System.out.println("\t-----------------------------");

i = 0;

for (Constructor cObj : srcObj.getClass().getDeclaredConstructors()) {

System.out.println("\tConstructor (" + (i++) + ") : " + cObj);

}

System.out.println("\n\n\tAvailable Methods Are ::");

System.out.println("\t------------------------");

i = 0;

for (Method cObj : srcObj.getClass().getDeclaredMethods()) {

System.out.println("\tMethod (" + (i++) + ") : " + cObj);

}

}

}

/**

* source class for getting data of its memebers

*

*/

class SourceClass {

// member variables

private int intVar;

private static final float PI = 3.14f;

// default constructor

SourceClass() {

intVar = 0;

}

// parameeterized constructor

SourceClass(int intV) {

intVar = intV;

}

// displays value of intVar

public void display() {

System.out.println("Value of intVar : " + intVar);

}

}

Followers