12
Sep

StringTokenizer example

import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class StrTokenizer {

	private static final String SEPARATOR = ".";

	public static void main(String[] args) {
		Version("s10.0.0.versionone");
	}

	public static void Version(String version) {
		int maj = 0;
		int min = 0;
		int mic = 0;
		String qual = "";

		try {
			StringTokenizer st = new StringTokenizer(version, SEPARATOR, true);
			maj = parseInt(st.nextToken(), version);

			if (st.hasMoreTokens()) { // minor
				st.nextToken(); // consume delimiter
				min = parseInt(st.nextToken(), version);

				if (st.hasMoreTokens()) { // micro
					st.nextToken(); // consume delimiter
					mic = parseInt(st.nextToken(), version);

					if (st.hasMoreTokens()) { // qualifier separator
						st.nextToken(); // consume delimiter
						qual = st.nextToken(""); // remaining string

						if (st.hasMoreTokens()) { // fail safe
							throw new IllegalArgumentException("invalid version \"" + version + "\": invalid format");
						}
					}
				}
			}
		} catch (NoSuchElementException e) {
			IllegalArgumentException iae = new IllegalArgumentException(
					"invalid version \"" + version + "\": invalid format");
			iae.initCause(e);
			throw iae;
		}
		System.out.println(maj +":"+min+":"+mic+":"+qual);
	}

	private static int parseInt(String value, String version) {
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			IllegalArgumentException iae = new IllegalArgumentException(
					"invalid version \"" + version + "\": non-numeric \"" + value + "\"");
			iae.initCause(e);
			throw iae;
		}
	}
}


================================================================================================================================================================