Example usage for java.util Scanner useLocale

List of usage examples for java.util Scanner useLocale

Introduction

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

Prototype

public Scanner useLocale(Locale locale) 

Source Link

Document

Sets this scanner's locale to the specified locale.

Usage

From source file:hu.sztaki.incremental.ml.streaming.imsr.MatrixVectorPairSource.java

private static Scanner initCsvScanner(Scanner s) {
    s.useLocale(Locale.ENGLISH);
    s.useDelimiter("(\\s+|\\s*,\\s*)");
    return s;//  w w  w.java2  s.  c o m
}

From source file:jfractus.app.Main.java

private static void parseSize(String sizeStr, Dimension dim)
        throws ArgumentParseException, BadValueOfArgumentException {
    Scanner scanner = new Scanner(sizeStr);
    scanner.useLocale(Locale.ENGLISH);
    scanner.useDelimiter("x");

    int outWidth = 0, outHeight = 0;
    try {//from  w w w. j  a  v  a2s  . co  m
        outWidth = scanner.nextInt();
        outHeight = scanner.nextInt();

        if (outWidth < 0 || outHeight < 0)
            throw new BadValueOfArgumentException("Bad value of argument");
    } catch (InputMismatchException e) {
        throw new ArgumentParseException("Command line parse exception");
    } catch (NoSuchElementException e) {
        throw new ArgumentParseException("Command line parse exception");
    }

    if (scanner.hasNext())
        throw new ArgumentParseException("Command line parse exception");

    dim.setSize(outWidth, outHeight);
}

From source file:com.planetmayo.debrief.satc.util.StraightLineCullingTestForm.java

private void loadFile(File file) throws IOException {
    Scanner scanner = new Scanner(file);
    scanner.useLocale(Locale.US);
    List<List<Coordinate>> coordinates = new ArrayList<List<Coordinate>>();
    while (true) {
        try {/*www.  j  a v  a2 s .  co  m*/
            int num = scanner.nextInt();
            scanner.next();
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();

            if (num > 0) {
                num--;
                while (coordinates.size() <= num) {
                    coordinates.add(new ArrayList<Coordinate>());
                }
                Coordinate coordinate = new Coordinate(x, y);
                coordinates.get(num).add(coordinate);
            }
        } catch (NoSuchElementException ex) {
            break;
        }
    }
    for (int i = 0; i < coordinates.size(); i++) {
        Coordinate c = coordinates.get(i).get(0);
        coordinates.get(i).add(c);
    }
    culling(coordinates);
}

From source file:fNIRs.FNIRsStats.java

private static Scanner makeScanner(File file) {
    FileReader reader;/*w  w  w.j  a  va 2 s  .c  o  m*/
    try { // the file may or may not exist at this point
        reader = new FileReader(file);
    } catch (FileNotFoundException fnf_exception) {
        // WE WILL PROBABLY WANT TO INTEGRATE THIS INTO THE GUI
        // Print the name of the missing file:
        localError(fnf_exception.getMessage());
        return null;
    } catch (Exception ex) {
        localError("unknown problem creating FileReader for \"" + file + "\": " + ex.getMessage());
        return null;
    }
    // create Scanner to read easily from the input files:
    Scanner s = new Scanner(new BufferedReader(reader));
    s.useLocale(Locale.US); // tell the scanner that numbers in the input
    // are formatted with decimal points, not
    // commas, etc.
    return s;
}

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//from www . ja v  a 2  s  . c om
 * 
 * @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.DriftCalculator.java

/**
 * Read the drift file storing the T,X,Y into the class level calculatedTimepoints, lastdx and lastdy
 * arrays. Ignore any records where T is outside the limits.
 * //  ww w  .j av  a  2s.  co  m
 * @param limits
 * @return The number of records read
 */
private int readDriftFile(int[] limits) {
    int ok = 0;
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(driftFilename);
        input = new BufferedReader(new UnicodeReader(fis, null));

        String line;
        Pattern pattern = Pattern.compile("[\t, ]+");
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (Character.isDigit(line.charAt(0))) {
                try {
                    Scanner scanner = new Scanner(line);
                    scanner.useDelimiter(pattern);
                    scanner.useLocale(Locale.US);
                    final int t = scanner.nextInt();
                    if (t < limits[0] || t > limits[1])
                        continue;
                    final double x = scanner.nextDouble();
                    final double y = scanner.nextDouble();
                    calculatedTimepoints[t] = ++ok;
                    lastdx[t] = x;
                    lastdy[t] = y;
                    scanner.close();
                } catch (InputMismatchException e) {
                } catch (NoSuchElementException e) {
                }
            }
        }
    } catch (IOException e) {
        // ignore
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
            // Ignore
        }
    }
    return ok;
}