Search in sources :

Example 61 with InputMismatchException

use of java.util.InputMismatchException in project robovm by robovm.

the class ScannerTest method test_hasNextBigInteger.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextBigInteger()
     */
public void test_hasNextBigInteger() throws IOException {
    s = new Scanner("123 456");
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("123"), s.nextBigInteger());
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("456"), s.nextBigInteger());
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    s.useRadix(5);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("38"), s.nextBigInteger());
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    /*
         * Different locale can only recognize corresponding locale sensitive
         * string. ',' is used in many locales as group separator.
         */
    s = new Scanner("23,456 23,456");
    s.useLocale(Locale.GERMANY);
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    /*
         * ''' is used in many locales as group separator.
         */
    s = new Scanner("23'456 23'456");
    s.useLocale(Locale.GERMANY);
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(new BigInteger("102"), s.nextBigInteger());
    s.useRadix(5);
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useRadix(10);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("162"), s.nextBigInteger());
    /*
         * '.' is used in many locales as group separator. The input string
         * has Arabic-Indic digits .
         */
    s = new Scanner("23.45٦ 23.456");
    s.useLocale(Locale.CHINESE);
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    assertFalse(s.hasNextBigInteger());
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("3456"), s.nextBigInteger());
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("3456"), s.nextBigInteger());
    s = new Scanner("E34");
    s.useRadix(16);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("3636"), s.nextBigInteger());
    /*
         * There are 3 types of zero digit in all locales, '0' '०' '๐'
         * respectively, but they are not differentiated.
         */
    s = new Scanner("12300");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("12300"), s.nextBigInteger());
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("12300"), s.nextBigInteger());
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("12300"), s.nextBigInteger());
    s = new Scanner("-123");
    s.useLocale(new Locale("ar", "AE"));
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("-123"), s.nextBigInteger());
    s = new Scanner("-123");
    s.useLocale(new Locale("mk", "MK"));
    assertTrue(s.hasNextBigInteger());
    assertEquals(new BigInteger("-123"), s.nextBigInteger());
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) BigInteger(java.math.BigInteger) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 62 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PCPALMFitting method loadCorrelationCurve.

/**
	 * Load a correlation curve from file. Will set the global gr, peakDensity and spatialDomain variables. If the data
	 * fails to be loaded then the method will return false.
	 * 
	 * @return True if loaded
	 */
private boolean loadCorrelationCurve() {
    inputFilename = Utils.getFilename("Input_Correlation_File", inputFilename);
    if (inputFilename == null)
        return false;
    // Set the analysis variables
    boolean spatialDomainSet = false;
    boolean peakDensitySet = false;
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(inputFilename);
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int count = 0;
        Pattern pattern = Pattern.compile("#([^=]+) = ([^ ]+)");
        // Read the header
        while ((line = input.readLine()) != null) {
            count++;
            if (line.length() == 0)
                continue;
            if (line.charAt(0) != '#') {
                // This is the first record
                break;
            }
            // This is a header line. Extract the key-value pair
            Matcher match = pattern.matcher(line);
            if (match.find()) {
                if (match.group(1).equals(HEADER_SPATIAL_DOMAIN)) {
                    // Do not use Boolean.parseBoolean because this will not fail if the field is 
                    // neither true/false - it only return true for a match to true
                    spatialDomainSet = true;
                    if (match.group(2).equalsIgnoreCase("true"))
                        spatialDomain = true;
                    else if (match.group(2).equalsIgnoreCase("false"))
                        spatialDomain = false;
                    else
                        // We want to know if the field is not true/false
                        spatialDomainSet = false;
                } else if (match.group(1).equals(HEADER_PEAK_DENSITY)) {
                    try {
                        peakDensity = Double.parseDouble(match.group(2));
                        peakDensitySet = true;
                    } catch (NumberFormatException e) {
                    // Ignore this.
                    }
                }
            }
        }
        if (!peakDensitySet) {
            IJ.error(TITLE, "No valid " + HEADER_PEAK_DENSITY + " record in file " + inputFilename);
            return false;
        }
        if (!spatialDomainSet) {
            IJ.error(TITLE, "No valid " + HEADER_SPATIAL_DOMAIN + " record in file " + inputFilename);
            return false;
        }
        // Read the data: gr[0][i], gr[1][i], gr[2][i]
        ArrayList<double[]> data = new ArrayList<double[]>();
        while (line != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) == '#')
                continue;
            // Extract the first 3 fields
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter("[\t ,]+");
            double r, g;
            try {
                r = scanner.nextDouble();
                g = scanner.nextDouble();
            } catch (InputMismatchException e) {
                IJ.error(TITLE, "Incorrect fields on line " + count);
                scanner.close();
                return false;
            } catch (NoSuchElementException e) {
                IJ.error(TITLE, "Incorrect fields on line " + count);
                scanner.close();
                return false;
            }
            // Allow the file to be missing the curve error. This is only used for plotting anyway.
            double error = 0;
            try {
                error = scanner.nextDouble();
            } catch (InputMismatchException e) {
            } catch (NoSuchElementException e) {
            }
            scanner.close();
            data.add(new double[] { r, g, error });
            // Read the next line
            line = input.readLine();
            count++;
        }
        if (data.isEmpty()) {
            IJ.error(TITLE, "No data in file " + inputFilename);
            return false;
        }
        gr = new double[3][data.size()];
        for (int i = 0; i < data.size(); i++) {
            final double[] d = data.get(i);
            gr[0][i] = d[0];
            gr[1][i] = d[1];
            gr[2][i] = d[2];
        }
    } catch (IOException e) {
        IJ.error(TITLE, "Unable to read from file " + inputFilename);
        return false;
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    return true;
}
Also used : Pattern(java.util.regex.Pattern) Scanner(java.util.Scanner) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) InputMismatchException(java.util.InputMismatchException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) NoSuchElementException(java.util.NoSuchElementException)

Example 63 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createPeakResultDeviationsV2.

private PeakResult createPeakResultDeviationsV2(String line) {
    try {
        float[] params = new float[7];
        float[] paramsStdDev = new float[7];
        if (isUseScanner()) {
            // Code using a Scanner
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(tabPattern);
            scanner.useLocale(Locale.US);
            int id = 0, endPeak = 0;
            if (readId)
                id = scanner.nextInt();
            int peak = scanner.nextInt();
            if (readEndFrame)
                endPeak = scanner.nextInt();
            int origX = scanner.nextInt();
            int origY = scanner.nextInt();
            float origValue = scanner.nextFloat();
            double chiSquared = scanner.nextDouble();
            float noise = scanner.nextFloat();
            for (int i = 0; i < params.length; i++) {
                params[i] = scanner.nextFloat();
                paramsStdDev[i] = scanner.nextFloat();
            }
            scanner.close();
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev);
        } else {
            // JUnit test shows this is faster than the scanner
            // Code using split and parse
            String[] fields = tabPattern.split(line);
            int j = 0;
            int id = (readId) ? Integer.parseInt(fields[j++]) : 0;
            int peak = Integer.parseInt(fields[j++]);
            int endPeak = (readEndFrame) ? Integer.parseInt(fields[j++]) : 0;
            int origX = Integer.parseInt(fields[j++]);
            int origY = Integer.parseInt(fields[j++]);
            float origValue = Float.parseFloat(fields[j++]);
            double chiSquared = Double.parseDouble(fields[j++]);
            float noise = Float.parseFloat(fields[j++]);
            for (int i = 0; i < params.length; i++) {
                params[i] = Float.parseFloat(fields[j++]);
                paramsStdDev[i] = Float.parseFloat(fields[j++]);
            }
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev);
        }
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    } catch (IndexOutOfBoundsException e) {
    } catch (NumberFormatException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 64 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createTableResultV2.

private PeakResult createTableResultV2(String line) {
    // [Precision] 
    try {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(tabPattern);
        scanner.useLocale(Locale.US);
        int id = 0, endPeak = 0;
        if (readId)
            id = scanner.nextInt();
        if (readSource)
            scanner.next();
        int peak = scanner.nextInt();
        if (readEndFrame)
            endPeak = scanner.nextInt();
        int origX = scanner.nextInt();
        int origY = scanner.nextInt();
        float origValue = scanner.nextFloat();
        double error = scanner.nextDouble();
        float noise = scanner.nextFloat();
        @SuppressWarnings("unused") float // Ignored but must be read
        snr = scanner.nextFloat();
        float[] params = new float[7];
        float[] paramsStdDev = (deviations) ? new float[7] : null;
        for (int i = 0; i < params.length; i++) {
            params[i] = scanner.nextFloat();
            if (deviations)
                paramsStdDev[i] = scanner.nextFloat();
        }
        scanner.close();
        if (readId || readEndFrame)
            return new ExtendedPeakResult(peak, origX, origY, origValue, error, noise, params, paramsStdDev, endPeak, id);
        else
            return new PeakResult(peak, origX, origY, origValue, error, noise, params, paramsStdDev);
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 65 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createPeakResultV2.

private PeakResult createPeakResultV2(String line) {
    try {
        float[] params = new float[7];
        if (isUseScanner()) {
            // Code using a Scanner
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(tabPattern);
            scanner.useLocale(Locale.US);
            int id = 0, endPeak = 0;
            if (readId)
                id = scanner.nextInt();
            int peak = scanner.nextInt();
            if (readEndFrame)
                endPeak = scanner.nextInt();
            int origX = scanner.nextInt();
            int origY = scanner.nextInt();
            float origValue = scanner.nextFloat();
            double chiSquared = scanner.nextDouble();
            float noise = scanner.nextFloat();
            for (int i = 0; i < params.length; i++) {
                params[i] = scanner.nextFloat();
            }
            scanner.close();
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, null, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, null);
        } else {
            // Code using split and parse
            String[] fields = tabPattern.split(line);
            int j = 0;
            int id = (readId) ? Integer.parseInt(fields[j++]) : 0;
            int peak = Integer.parseInt(fields[j++]);
            int endPeak = (readEndFrame) ? Integer.parseInt(fields[j++]) : 0;
            int origX = Integer.parseInt(fields[j++]);
            int origY = Integer.parseInt(fields[j++]);
            float origValue = Float.parseFloat(fields[j++]);
            double chiSquared = Double.parseDouble(fields[j++]);
            float noise = Float.parseFloat(fields[j++]);
            for (int i = 0; i < params.length; i++) {
                params[i] = Float.parseFloat(fields[j++]);
            }
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, null, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, null);
        }
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    } catch (IndexOutOfBoundsException e) {
    } catch (NumberFormatException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

InputMismatchException (java.util.InputMismatchException)83 Scanner (java.util.Scanner)56 NoSuchElementException (java.util.NoSuchElementException)47 Locale (java.util.Locale)23 BufferUnderflowException (java.nio.BufferUnderflowException)5 Pattern (java.util.regex.Pattern)5 BufferedReader (java.io.BufferedReader)4 BigInteger (java.math.BigInteger)4 UnicodeReader (gdsc.core.utils.UnicodeReader)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TodoList (de.djuelg.neuronizer.domain.model.preview.TodoList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)2 Point (java.awt.Point)2 Serializable (java.io.Serializable)2 BigDecimal (java.math.BigDecimal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1