Parse Float from String with default value - Java java.lang

Java examples for java.lang:String Parse

Description

Parse Float from String with default value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        System.out.println(getFloat(value));
    }/*from ww  w . ja  v  a  2  s. c  o  m*/

    public static Float getFloat(String value, Float defaultValue) {
        try {
            return Float.valueOf(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Float getFloat(String value) {
        return getFloat(value, null);
    }
}

Related Tutorials