Example usage for java.util Scanner nextFloat

List of usage examples for java.util Scanner nextFloat

Introduction

In this page you can find the example usage for java.util Scanner nextFloat.

Prototype

public float nextFloat() 

Source Link

Document

Scans the next token of the input as a float .

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";
    Float f = 1.2385f;//from   w  ww. jav a2 s  .c  o  m
    s = s + f;

    Scanner scanner = new Scanner(s);

    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        scanner.next();
        if (scanner.hasNextFloat()) {
            System.out.println("Found :" + scanner.nextFloat());
        }
    }

    scanner.close();
}

From source file:br.edu.ifes.bd2hibernate.cgt.Menu.java

private Object scanSpecific(String fieldName, Scanner sc, Class c) throws NoSuchFieldException {

    Field field = c.getDeclaredField(fieldName);
    boolean valid = false;
    Object value = null;//w  w w  .j a  v  a  2s  . c om

    while (!valid) {
        System.out.println(this.messages.get(fieldName.toUpperCase()));
        String type = field.getType().getSimpleName();

        switch (type.toUpperCase()) {
        case "STRING":
            value = sc.nextLine();
            valid = true;
            break;
        case "DATE":
            String date = sc.nextLine();
            valid = this.dateValidator.validate(date);
            if (valid)
                value = convertStringToDate(date);
            break;
        case "INT":
            value = sc.nextInt();
            valid = true;
            break;
        case "FLOAT":
            value = sc.nextFloat();
            valid = true;
            break;
        }
    }

    return value;
}

From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMClusters.java

/**
 * Load the histogram from the file. Assumes the histogram is [int, float] format and creates a contiguous histogram
 * from zero/* w  ww  .j  a  va  2s  .co  m*/
 * 
 * @param filename
 * @return
 */
private HistogramData loadHistogram(String filename) {
    BufferedReader input = null;
    try {
        int f = 0;
        double a = 0;
        String u = "";

        FileInputStream fis = new FileInputStream(filename);
        input = new BufferedReader(new UnicodeReader(fis, null));

        String line;
        int count = 0;

        ArrayList<float[]> data = new ArrayList<float[]>();

        // Read the header and store the calibration if present
        while ((line = input.readLine()) != null) {
            count++;
            if (line.length() == 0)
                continue;
            if (Character.isDigit(line.charAt(0)))
                // This is the first record
                break;
            String[] fields = line.split("[\t, ]+");
            if (fields[0].equalsIgnoreCase("frames"))
                f = Integer.parseInt(fields[1]);
            if (fields[0].equalsIgnoreCase("area"))
                a = Double.parseDouble(fields[1]);
            if (fields[0].equalsIgnoreCase("units"))
                u = fields[1];
        }

        final Pattern pattern = Pattern.compile("[\t, ]+");
        while (line != null) {
            if (line.length() == 0)
                continue;
            if (!Character.isDigit(line.charAt(0)))
                continue;

            // Extract the first 2 fields
            Scanner scanner = new Scanner(line);
            scanner.useLocale(Locale.US);
            scanner.useDelimiter(pattern);

            try {
                int molecules = scanner.nextInt();
                float frequency = scanner.nextFloat();

                // Check for duplicates
                for (float[] d : data) {
                    if (d[0] == molecules) {
                        error("Duplicate molecules field on line " + count);
                        return null;
                    }
                }

                data.add(new float[] { molecules, frequency });
            } catch (InputMismatchException e) {
                error("Incorrect fields on line " + count);
                return null;
            } catch (NoSuchElementException e) {
                error("Incorrect fields on line " + count);
                return null;
            } finally {
                scanner.close();
            }

            // Get the next line
            line = input.readLine();
            count++;
        }

        if (data.isEmpty()) {
            error("No data in file " + filename);
            return null;
        }

        // Create a contiguous histogram from zero
        int maxN = 0;
        for (float[] d : data) {
            if (maxN < d[0])
                maxN = (int) d[0];
        }

        float[][] hist = new float[2][maxN + 1];
        for (int n = 0; n <= maxN; n++) {
            hist[0][n] = n;
            for (float[] d : data) {
                if (n == d[0])
                    hist[1][n] = d[1];
            }
        }
        HistogramData histogramData = new HistogramData(hist, f, a, u);
        histogramData.filename = filename;
        return histogramData;
    } catch (IOException e) {
        IJ.error(TITLE, "Unable to read from file " + filename);
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
            // Ignore
        }
    }
    return null;
}

From source file:gdsc.smlm.ij.plugins.SpotAnalysis.java

private boolean resultsWithinBounds(Rectangle bounds) {
    if (resultsWindowShowing()) {
        float minx = bounds.x;
        float maxx = minx + bounds.width;
        float miny = bounds.y;
        float maxy = miny + bounds.height;

        for (int i = 0; i < resultsWindow.getTextPanel().getLineCount(); i++) {
            String line = resultsWindow.getTextPanel().getLine(i);
            Scanner s = new Scanner(line);
            s.useDelimiter("\t");
            s.nextInt();/*from w  w  w.j  ava2 s  .  com*/
            float cx = s.nextFloat(); // cx
            float cy = s.nextFloat(); // cy
            s.close();
            if (cx >= minx && cx <= maxx && cy >= miny && cy <= maxy) {
                return true;
            }
        }
    }
    return false;
}