Example usage for java.lang Float parseFloat

List of usage examples for java.lang Float parseFloat

Introduction

In this page you can find the example usage for java.lang Float parseFloat.

Prototype

public static float parseFloat(String s) throws NumberFormatException 

Source Link

Document

Returns a new float initialized to the value represented by the specified String , as performed by the valueOf method of class Float .

Usage

From source file:ai.susi.server.api.vis.PieChartServlet.java

public JFreeChart getChart(JSONObject jsonData, boolean legendBit, boolean tooltipBit) {
    DefaultPieDataset dataset = new DefaultPieDataset();
    Iterator<String> iter = jsonData.keys();

    while (iter.hasNext()) {
        String keyData = iter.next();
        Float value = Float.parseFloat(jsonData.getString(keyData));
        dataset.setValue(keyData, value);
    }/*from   www .j  av  a2  s .c  om*/

    boolean legend = legendBit;
    boolean tooltips = tooltipBit;
    boolean urls = false;

    JFreeChart chart = ChartFactory.createPieChart("Loklak Visualizes - PieChart", dataset, legend, tooltips,
            urls);

    chart.setBorderPaint(Color.BLACK);
    chart.setBorderStroke(new BasicStroke(5.0f));
    chart.setBorderVisible(true);

    return chart;
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * Uses reflection to dynamically set a parameter bean's parameters from a given map of Strings.
 * /*from  www . ja v  a  2  s .c o m*/
 * @param parameterMap
 *          Mapping of parameter names (attributes) to values.
 * @param parameterObject
 *          Java bean to set parameter values of of. Setter methods must conform to the Java bean naming scheme and must take in one argument of
 *          type String, Integer, Long, Float, or Double.
 * @exception IllegalArgumentException
 *              Thrown if a given setter method cannot be found or if the setter method does not conform to the rules described.
 */
@SuppressWarnings("unchecked")
public static void setParametersFromMap(final Map parameterMap, final Object parameterObject) {
    final Method[] methods = parameterObject.getClass().getMethods();
    final Map<String, Method> objectSetMethodMap = new HashMap<String, Method>(methods.length);

    for (final Method method : parameterObject.getClass().getMethods()) {
        final String methodName = method.getName();
        if (methodName.contains("set")) {
            objectSetMethodMap.put(method.getName(), method);
        }
    }

    for (final Object keyObject : parameterMap.keySet()) {
        final String key = (String) keyObject;
        final String value = (String) parameterMap.get(key);
        final String setterMethodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
        final Method setterMethod = objectSetMethodMap.get(setterMethodName);
        if (setterMethod == null) {
            continue;
        }
        final Class<?>[] parameterTypes = setterMethod.getParameterTypes();
        if (parameterTypes.length != 1) {
            throw new IllegalArgumentException(
                    "Illegal setter method found, must take in exactly one argument.");
        }
        final Class<?> argumentType = parameterTypes[0];
        try {
            if (argumentType.equals(String.class)) {
                setterMethod.invoke(parameterObject, value);
                continue;
            }
            if (value == null || value.trim().equals("")) {
                setterMethod.invoke(parameterObject, (Object) null);
                continue;
            }
            if (argumentType.equals(Double.class) || argumentType.equals(double.class)) {
                setterMethod.invoke(parameterObject, Double.parseDouble(value));
            } else if (argumentType.equals(Integer.class) || argumentType.equals(int.class)) {
                setterMethod.invoke(parameterObject, Integer.parseInt(value));
            } else if (argumentType.equals(Long.class) || argumentType.equals(long.class)) {
                setterMethod.invoke(parameterObject, Long.parseLong(value));
            } else if (argumentType.equals(Float.class) || argumentType.equals(float.class)) {
                setterMethod.invoke(parameterObject, Float.parseFloat(value));
            } else if (argumentType.equals(Boolean.class) || argumentType.equals(boolean.class)) {
                setterMethod.invoke(parameterObject, Boolean.parseBoolean(value));
            } else if (argumentType.equals(Short.class) || argumentType.equals(short.class)) {
                setterMethod.invoke(parameterObject, Short.parseShort(value));
            } else {
                throw new IllegalArgumentException("Illegal type found for argument to setter bean - type is "
                        + argumentType + " - key is " + key);
            }
        } catch (final Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:juicebox.tools.utils.common.ArrayTools.java

public static float[] extractFloats(List<String> stringList) {
    float[] array = new float[stringList.size()];

    int index = 0;
    for (String val : stringList) {
        array[index] = Float.parseFloat(val);
        index++;//from w w  w .jav  a  2 s .  c  om
    }
    return array;
}

From source file:io.starter.datamodel.Sys.java

/**
 * add a counter to the system stats/*  ww w  .j  a  v a  2 s  . co  m*/
 * 
 * @param name
 * @param ct
 */
public void incrementCounter(String name, Object ct) {

    Object existing = counters.get(name);
    if (existing != null) {
        // attempt to treat as a number, fail to String
        try {
            if (existing instanceof Float) {
                float fnf = Float.parseFloat(existing.toString());
                float fnadd = Float.parseFloat(ct.toString());
                // increment by new value
                fnf += fnadd;
                // update the counter
                counters.put(name, new Float(fnf));

            } else if (existing instanceof Integer) {
                int fni = Integer.parseInt(existing.toString());
                int fnadd = Integer.parseInt(ct.toString());
                // increment by new value
                fni += fnadd;
                // update the counter
                counters.put(name, new Integer(fni));
            }
            // is it an integer or a real float?
        } catch (NumberFormatException xf) {
            // cannot be "added", if it's a string append or replace?
            counters.put(name, ct);
        }
    } else {
        counters.put(name, ct);
    }
}

From source file:com.igormaznitsa.ideamindmap.facet.InMemoryPreferenceNode.java

@Override
public float getFloat(String key, float def) {
    final String value = get(key, Float.toString(def));
    try {/*from  w w w  .  ja v  a2  s  . c  o m*/
        return Float.parseFloat(value);
    } catch (NumberFormatException ex) {
        return def;
    }
}

From source file:com.ratebeer.android.api.command.GetUserRatingsCommand.java

@Override
protected void parse(String html) throws JSONException, ApiException {

    // Successful response?
    if (html.indexOf("Beer Ratings</a></span>") < 0) {
        throw new ApiException(ApiException.ExceptionType.CommandFailed,
                "The response HTML did not contain the unique ratings table begin HTML string");
    }// www  .  j av a2s  .  co m

    // Maybe no ratings?
    int tableStart = html.indexOf("<!-- RATINGS -->");
    if (tableStart < 0) {
        ratings = new ArrayList<UserRating>();
        return;
    }

    // Parse the beer ratings table
    String rowText = "><A HREF=\"/beer/";
    int rowStart = html.indexOf(rowText, tableStart) + rowText.length();
    ratings = new ArrayList<UserRating>();

    while (rowStart > 0 + rowText.length()) {

        int idStart = html.indexOf("/", rowStart) + 1;
        int beerId = Integer.parseInt(html.substring(idStart, html.indexOf("/", idStart)));

        int beerStart = html.indexOf(">", idStart) + 1;
        String beerName = HttpHelper.cleanHtml(html.substring(beerStart, html.indexOf("<", beerStart)));

        int brewerStart = html.indexOf("/\">", beerStart) + 3;
        String brewerName = HttpHelper.cleanHtml(html.substring(brewerStart, html.indexOf("<", brewerStart)));

        int styleStart = html.indexOf("owrap>", brewerStart) + "owrap>".length();
        String styleName = HttpHelper.cleanHtml(html.substring(styleStart, html.indexOf("</", styleStart)));

        int scoreStart = html.indexOf("center>", styleStart) + "center>".length();
        float score = Float.parseFloat(html.substring(scoreStart, html.indexOf("<", scoreStart)));

        int myRatingStart = html.indexOf("center>", scoreStart) + "center>".length();
        float myRating = Float.parseFloat(html.substring(myRatingStart, html.indexOf("<", myRatingStart)));

        int dateStart = html.indexOf("align=right>", myRatingStart) + "align=right>".length();
        Date date = new Date();
        try {
            date = DATE_FORMATTER.parse(html.substring(dateStart, html.indexOf("&", dateStart)));
        } catch (ParseException e) {
        }

        ratings.add(new UserRating(beerId, beerName, brewerName, styleName, score, myRating, date));
        rowStart = html.indexOf(rowText, dateStart) + rowText.length();
    }

}

From source file:com.clican.pluto.common.resource.MapResource.java

public float getFloat(String key) {
    String s = get(key);/*from w  ww  .java  2 s  .  co m*/
    try {
        return Float.parseFloat(s);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Property must be an float value :" + key);
    }
}

From source file:org.uimafit.factory.ConfigurationParameterFactory.java

/**
 * Determines the default value of an annotated configuration parameter. The returned value is
 * not necessarily the value that the annotated member variable will be instantiated with in
 * ConfigurationParameterInitializer which does extra work to convert the UIMA configuration
 * parameter value to comply with the type of the member variable.
 *//*from ww w. ja v  a  2s .  co m*/
public static Object getDefaultValue(Field field) {
    if (isConfigurationParameterField(field)) {
        org.uimafit.descriptor.ConfigurationParameter annotation = field
                .getAnnotation(org.uimafit.descriptor.ConfigurationParameter.class);

        String[] stringValue = annotation.defaultValue();
        if (stringValue.length == 1
                && stringValue[0].equals(org.uimafit.descriptor.ConfigurationParameter.NO_DEFAULT_VALUE)) {
            return null;
        }

        String valueType = getConfigurationParameterType(field);
        boolean isMultiValued = isMultiValued(field);

        if (!isMultiValued) {
            if (ConfigurationParameter.TYPE_BOOLEAN.equals(valueType)) {
                return Boolean.parseBoolean(stringValue[0]);
            } else if (ConfigurationParameter.TYPE_FLOAT.equals(valueType)) {
                return Float.parseFloat(stringValue[0]);
            } else if (ConfigurationParameter.TYPE_INTEGER.equals(valueType)) {
                return Integer.parseInt(stringValue[0]);
            } else if (ConfigurationParameter.TYPE_STRING.equals(valueType)) {
                return stringValue[0];
            }
            throw new UIMA_IllegalArgumentException(
                    UIMA_IllegalArgumentException.METADATA_ATTRIBUTE_TYPE_MISMATCH,
                    new Object[] { valueType, "type" });
        } else {
            if (ConfigurationParameter.TYPE_BOOLEAN.equals(valueType)) {
                Boolean[] returnValues = new Boolean[stringValue.length];
                for (int i = 0; i < stringValue.length; i++) {
                    returnValues[i] = Boolean.parseBoolean(stringValue[i]);
                }
                return returnValues;
            } else if (ConfigurationParameter.TYPE_FLOAT.equals(valueType)) {
                Float[] returnValues = new Float[stringValue.length];
                for (int i = 0; i < stringValue.length; i++) {
                    returnValues[i] = Float.parseFloat(stringValue[i]);
                }
                return returnValues;
            } else if (ConfigurationParameter.TYPE_INTEGER.equals(valueType)) {
                Integer[] returnValues = new Integer[stringValue.length];
                for (int i = 0; i < stringValue.length; i++) {
                    returnValues[i] = Integer.parseInt(stringValue[i]);
                }
                return returnValues;
            } else if (ConfigurationParameter.TYPE_STRING.equals(valueType)) {
                return stringValue;
            }
            throw new UIMA_IllegalArgumentException(
                    UIMA_IllegalArgumentException.METADATA_ATTRIBUTE_TYPE_MISMATCH,
                    new Object[] { valueType, "type" });

        }

    } else {
        throw new IllegalArgumentException("field is not annotated with annotation of type "
                + org.uimafit.descriptor.ConfigurationParameter.class.getName());
    }
}

From source file:com.luhuiguo.cordova.voice.VoiceHandler.java

/**
 * Executes the request and returns PluginResult.
 * @param action        The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackContext       The callback context used when calling back into JavaScript.
 * @return              A PluginResult object with a status and message.
 *//*from   w ww.  ja va2s.c  o  m*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    CordovaResourceApi resourceApi = webView.getResourceApi();
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    if (action.equals("startRecording")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }
        this.startRecording(args.getString(0), VoiceHandler.stripFileProtocol(fileUriStr));
    } else if (action.equals("stopRecording")) {
        this.stopRecording(args.getString(0));
    } else if (action.equals("startPlaying")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }
        this.startPlaying(args.getString(0), VoiceHandler.stripFileProtocol(fileUriStr));
    } else if (action.equals("seekTo")) {
        this.seekTo(args.getString(0), args.getInt(1));
    } else if (action.equals("pausePlaying")) {
        this.pausePlaying(args.getString(0));
    } else if (action.equals("stopPlaying")) {
        this.stopPlaying(args.getString(0));
    } else if (action.equals("setVolume")) {
        try {
            this.setVolume(args.getString(0), Float.parseFloat(args.getString(1)));
        } catch (NumberFormatException nfe) {
            //no-op
        }
    } else if (action.equals("getCurrentPosition")) {
        float f = this.getCurrentPosition(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, f));
        return true;
    } else if (action.equals("getPower")) {
        float f = this.getPower(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, f));
        return true;
    } else if (action.equals("getDuration")) {
        float f = this.getDuration(args.getString(0), args.getString(1));
        callbackContext.sendPluginResult(new PluginResult(status, f));
        return true;
    } else if (action.equals("create")) {
        String id = args.getString(0);
        String src = VoiceHandler.stripFileProtocol(args.getString(1));
        VoicePlayer voice = new VoicePlayer(this, id, src);
        this.players.put(id, voice);
    } else if (action.equals("release")) {
        boolean b = this.release(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, b));
        return true;
    } else { // Unrecognized action.
        return false;
    }

    callbackContext.sendPluginResult(new PluginResult(status, result));

    return true;
}

From source file:com.qframework.core.ItemFactory.java

public GameonModel getFromTemplate(String strType, String strData, String strColor) {

    if (mModels.containsKey(strData)) {
        return mModels.get(strData);
    }/*from   w ww.java 2  s. com*/

    int textid = mApp.textures().mTextureDefault;
    GLColor color = null;
    if (strColor == null) {
        color = mApp.colors().white;
    } else {
        color = mApp.colors().getColor(strColor);
    }

    float[] grid = new float[3];
    grid[0] = 1;
    grid[1] = 1;
    grid[2] = 1;

    String template = null;
    StringTokenizer tok = new StringTokenizer(strData, ".");
    if (tok.countTokens() == 1) {
        template = strData;
    } else {
        template = tok.nextToken();
        grid[0] = Float.parseFloat(tok.nextToken());
        grid[1] = Float.parseFloat(tok.nextToken());
        grid[2] = Float.parseFloat(tok.nextToken());
    }

    if (template.equals("sphere")) {
        GameonModel model = createFromType(GameonModelData.Type.SPHERE, color, textid, grid);
        model.mModelTemplate = GameonModelData.Type.SPHERE;
        model.mIsModel = true;
        return model;

    } else if (template.equals("cube")) {
        //GameonModel model = new GameonModel(strData , mApp);
        //model.createObject(8,8);
        GameonModel model = createFromType(GameonModelData.Type.CUBE, color, textid, grid);
        model.mModelTemplate = GameonModelData.Type.CUBE;
        model.mIsModel = true;
        return model;

    }

    GameonModel model = new GameonModel(template, mApp, null);
    if (template.equals("cylinder")) {
        model.createModel(GameonModelData.Type.CYLINDER, textid, color, grid);
        model.mModelTemplate = GameonModelData.Type.CYLINDER;
        model.mIsModel = true;
    } else if (template.equals("plane")) {
        model.createPlane(-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, color, grid);
        model.mModelTemplate = GameonModelData.Type.CYLINDER;
        model.mIsModel = true;
    } else if (template.equals("card52")) {
        model.createCard2(-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, color);
        model.mModelTemplate = GameonModelData.Type.CARD52;
        model.mForceHalfTexturing = true;
        model.mForcedOwner = 32;
        model.mHasAlpha = true;
        model.mIsModel = true;
    } else if (template.equals("cardbela")) {
        model.createCard(-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, mApp.colors().transparent);
        model.mModelTemplate = GameonModelData.Type.CARD52;
        model.mForceHalfTexturing = true;
        model.mForcedOwner = 32;
        model.mHasAlpha = true;
        model.mIsModel = true;
    } else if (template.equals("background")) {
        model.createPlane(-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, color, grid);
        model.mModelTemplate = GameonModelData.Type.BACKGROUND;
        model.mHasAlpha = true;
        model.mIsModel = false;

    } else {
        return null;
    }

    model.setTexture(textid);
    return model;

}