Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Color;
import java.util.StringTokenizer;

public class Main {
    public static Color convertHexStringToColor(String str) throws NumberFormatException {
        int multiplier = 1;
        StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+");
        while (tokenizer.hasMoreTokens()) {
            multiplier = 1;
            String token = tokenizer.nextToken();
            if (null == token) {
                throw new NumberFormatException(str);
            }
            if (token.startsWith("-")) {
                multiplier = -1;
                token = token.substring(1);
            }
            int point_index = token.indexOf(".");
            if (point_index > 0) {
                token = token.substring(0, point_index);
            } else if (point_index == 0) {
                return new Color(0);
            }
            try {
                if (token.startsWith("0x")) {
                    return new Color(multiplier * Integer.parseInt(token.substring(2), 16));
                } else if (token.startsWith("#")) {
                    return new Color(multiplier * Integer.parseInt(token.substring(1), 16));
                } else if (token.startsWith("0") && !token.equals("0")) {
                    return new Color(multiplier * Integer.parseInt(token.substring(1), 8));
                } else {
                    return new Color(multiplier * Integer.parseInt(token));
                }
            } catch (NumberFormatException e) {
                continue;
            }
        }
        throw new NumberFormatException(str);

    }
}