Example usage for java.lang Character valueOf

List of usage examples for java.lang Character valueOf

Introduction

In this page you can find the example usage for java.lang Character valueOf.

Prototype

@HotSpotIntrinsicCandidate
public static Character valueOf(char c) 

Source Link

Document

Returns a Character instance representing the specified char value.

Usage

From source file:Main.java

public static void main(String[] args) {
    char ch1 = 'i', ch2 = 65;

    Character c1 = Character.valueOf(ch1);
    Character c2 = Character.valueOf(ch2);

    String str1 = "Character value of " + ch1 + " is " + c1;
    String str2 = "Character value of " + ch2 + " is " + c2;

    System.out.println(str1);//  w  w w  . ja v  a  2  s  .  co  m
    System.out.println(str2);
}

From source file:Main.java

public static void main(String[] args) {

    String s = "R2D2C3P0";
    int result = s.chars().filter(Character::isDigit).map(ch -> Character.valueOf((char) ch)).sum();

    System.out.println(result);//  w w w.  java 2s  .  c o m
}

From source file:Main.java

public static void main(String[] args) {
    // Using the constructor
    Character c1 = new Character('A');

    // Using the factory method - preferred
    Character c2 = Character.valueOf('2');
    Character c3 = Character.valueOf('-');

    // Getting the wrapped char values
    char cc1 = c1.charValue();
    char cc2 = c2.charValue();
    char cc3 = c3.charValue();

    System.out.println("c1 = " + c1);
    System.out.println("c2 = " + c2);
    System.out.println("c3 = " + c3);

    // Using some Character class methods on c1
    System.out.println("isLowerCase c1    = " + Character.isLowerCase(cc1));
    System.out.println("isDigit c1    = " + Character.isDigit(cc1));
    System.out.println("isLetter c1    = " + Character.isLetter(cc1));
    System.out.println("Lowercase of  c1    = " + Character.toLowerCase(cc1));

    // Using some Character class methods on c2
    System.out.println("isLowerCase c2    = " + Character.isLowerCase(cc2));
    System.out.println("isDigit c2    = " + Character.isDigit(cc2));
    System.out.println("isLetter c2    = " + Character.isLetter(cc2));
    System.out.println("Lowercase of  c2    = " + Character.toLowerCase(cc2));

    System.out.println("Uppercase of  c3    = " + Character.toUpperCase(cc3));

}

From source file:markov.java

/**
 * @param args/*  w  ww  . j av  a  2s .com*/
 */
public static void main(String[] args) {

    // hack: eclipse don't support IO redirection worth a shit
    // try {
    // System.setIn(new FileInputStream("./json"));
    // } catch (FileNotFoundException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }

    boolean graphMode = false;
    boolean jsonMode = false;
    boolean jsonRecoverMode = false;
    boolean endNode = false;

    int count = -1;

    long n = 0;
    long sumOfSqr = 0;
    long sum = 0;

    for (String s : args) {

        if (!s.matches("^-[vegjJh]*(c[0-9]*)?$")) {
            System.out.println("invalid argument");
            return;
        }

        if (s.matches("^-.*h.*")) {
            System.out.println(HELP);
            return;
        }
        if (s.matches("^-.*v.*")) {
            verbose = true;
            log("verbose mode");
        }
        if (s.matches("^-.*g.*")) {
            graphMode = true;
            log("graph mode");
        }
        if (s.matches("^-.*j.*")) {
            jsonMode = true;
            log("json mode");
        }
        if (s.matches("^-.*J.*")) {
            jsonRecoverMode = true;
            log("json recover mode");
        }
        if (s.matches("^-.*e.*")) {
            endNode = true;
            log("include end node");
        }
        if (s.matches("^-.*c[0-9]*$")) {
            log("counted output mode");
            count = Integer.parseInt(s.replaceAll("^-.*c", ""));
        }

        boolean error = (graphMode == true && jsonMode == true);
        if (!error) {
            error = (count > -1) && (graphMode == true || jsonMode == true);
        }

        if (error) {
            System.err.println("[error] switches j, g and, c are mutualy exclusive.");
            return;
        }

    }

    StateTransitionDiagram<Character> std;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    try {
        if (!jsonRecoverMode) {
            Trainer<Character> trainer = new Trainer<Character>();
            String s = br.readLine();
            while (s != null) {
                trainer.train(string2List(s));
                n++;
                sumOfSqr += s.length() * s.length();
                sum += s.length();
                s = br.readLine();
            }
            if (n == 0) {
                System.err
                        .println("Invalid corpus: At least one sample is required, two to make it interesting");
                return;
            }
            std = trainer.getTransitionDiagram();
        } else {
            std = new StateTransitionDiagram<Character>();
            GsonStub gstub = new Gson().fromJson(br, GsonStub.class);
            n = gstub.meta.n;
            sum = gstub.meta.sum;
            sumOfSqr = gstub.meta.sumOfSqr;

            for (Entry<String, StateStub> entry : gstub.states.entrySet()) {
                State<Character> state;
                if (entry.getKey().equals("null")) {
                    state = std.getGuard();
                } else {
                    state = std.getState(Character.valueOf(entry.getKey().charAt(0)));
                }
                for (Entry<String, Integer> transitions : entry.getValue().transitions.entrySet()) {
                    State<Character> tranny;
                    if (transitions.getKey().equals("null")) {
                        tranny = std.getGuard();
                    } else {
                        tranny = std.getState(Character.valueOf(transitions.getKey().charAt(0)));
                    }

                    state.addTransition(tranny.getValue(), transitions.getValue());
                }
            }
        }
        if (graphMode) {
            if (endNode) {
                System.out.println(std.toString());
            } else {
                System.out.println(std.removeEndGuards().toString());
            }
            return;
        }
        if (jsonMode) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

            String partialJson;
            if (endNode) {
                partialJson = gson.toJson(std);
            } else {
                partialJson = gson.toJson(std.removeEndGuards());
            }
            GsonStub gstub = new Gson().fromJson(partialJson, GsonStub.class);
            gstub.meta = new Meta();
            gstub.meta.n = n;
            gstub.meta.sum = sum;
            gstub.meta.sumOfSqr = sumOfSqr;

            System.out.println(gson.toJson(gstub));
            return;
        }

        Generator<Character> generator;
        if (endNode) {
            generator = new EndTagGenerator<Character>(std);
        } else {
            double sd = ((double) sumOfSqr - (double) (sum * sum) / (double) n) / (double) (n - 1);
            double mean = (double) sum / (double) n;
            log(String.format("mean: %.4f sd: %.4f", mean, sd));
            NormalDistributionImpl dist = new NormalDistributionImpl(mean, sd);
            generator = new NormalizedGenerator<Character>(std.removeEndGuards(), dist);
        }
        if (count >= 0) {
            for (int c = 0; c < count; c++) {
                output(generator);
            }
        } else {
            while (true) {
                output(generator);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean isGB2312(char c) {
    Character ch = Character.valueOf(c);
    String sCh = ch.toString();//from w w  w  .  j av a 2 s  .c o m
    try {
        byte[] bb = sCh.getBytes("gb2312");
        if (bb.length > 1) {
            return true;
        }
    } catch (java.io.UnsupportedEncodingException ex) {
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isGB2312(char c) {
    Character ch = Character.valueOf(c);
    String sCh = ch.toString();/*w w  w .  ja v  a  2  s .  co  m*/
    try {
        byte[] bb = sCh.getBytes("gb2312");
        if (bb.length > 1) {
            return true;
        }
    } catch (UnsupportedEncodingException ex) {
        return false;
    }
    return false;
}

From source file:Main.java

private static String convertHexToChar(String hexchar) {
    try {//from  w w w  . jav  a 2s . c o m
        int val = Integer.parseInt(hexchar, 16);
        Character charVal = Character.valueOf((char) val);
        return charVal.toString();
    } catch (Exception e) {
    }
    return hexchar;
}

From source file:Main.java

public static void staticMethod() {
    Integer intObj1 = Integer.valueOf(100);
    Integer intObj2 = Integer.valueOf("2014");
    Double doubleObj1 = Double.valueOf(10.45);
    Double doubleObj2 = Double.valueOf("234.56");
    Character charObj1 = Character.valueOf('A');
}

From source file:Main.java

/**
 * box the char to Character
 */
public static Character boxed(char v) {
    return Character.valueOf(v);
}

From source file:Main.java

private static String unescape(String str) {
    StringBuilder sb = new StringBuilder();
    String[] codeStrs = str.split("\\\\u");
    for (String codeStr : codeStrs) {
        if (codeStr.isEmpty()) {
            continue;
        }//  w w  w  .j av a 2 s.  com
        try {
            if (codeStr.length() <= 4) {
                sb.append(Character.valueOf((char) Integer.parseInt(codeStr, 16)));
            } else {
                sb.append(Character.valueOf((char) Integer.parseInt(codeStr.substring(0, 4), 16)));
                sb.append(codeStr.substring(4));
            }
        } catch (NumberFormatException e) {
            sb.append(codeStr);
        }
    }
    return sb.toString();
}