Java Code Examples for java.util.Scanner#useLocale()

The following examples show how to use java.util.Scanner#useLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckPanel.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
private double valueOf (String text)
{
    Scanner scanner = new Scanner(text);
    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        if (scanner.hasNextDouble()) {
            return scanner.nextDouble();
        } else {
            scanner.next();
        }
    }

    // Kludge!
    return Double.NaN;
}
 
Example 2
Source File: CheckPanel.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
private double valueOf (String text)
{
    Scanner scanner = new Scanner(text);
    scanner.useLocale(Locale.getDefault());

    while (scanner.hasNext()) {
        if (scanner.hasNextDouble()) {
            return scanner.nextDouble();
        } else {
            scanner.next();
        }
    }

    // Kludge!
    return Double.NaN;
}
 
Example 3
Source File: ProcessInformations.java    From javamelody with Apache License 2.0 6 votes vote down vote up
public static List<ProcessInformations> buildProcessInformations(InputStream in,
		boolean windows, boolean macOrAix) {
	final String charset;
	if (windows) {
		charset = "Cp1252";
	} else {
		charset = "UTF-8";
	}
	final Scanner sc = new Scanner(in, charset);
	sc.useRadix(10);
	sc.useLocale(Locale.US);
	sc.nextLine();
	if (windows) {
		sc.nextLine();
		sc.nextLine();
	}

	final List<ProcessInformations> processInfos = new ArrayList<ProcessInformations>();
	while (sc.hasNext()) {
		final ProcessInformations processInfo = new ProcessInformations(sc, windows, macOrAix);
		processInfos.add(processInfo);
	}
	return Collections.unmodifiableList(processInfos);
}
 
Example 4
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an input stream from a URL.
 */
public In(URL url) {
    try {
        URLConnection site = url.openConnection();
        InputStream is     = site.getInputStream();
        scanner            = new Scanner(new BufferedInputStream(is), charsetName);
        scanner.useLocale(usLocale);
    }
    catch (IOException ioe) {
        System.err.println("Could not open " + url);
    }
}
 
Example 5
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an input stream from a file.
 */
public In(File file) {
    try {
        scanner = new Scanner(file, charsetName);
        scanner.useLocale(usLocale);
    }
    catch (IOException ioe) {
        System.err.println("Could not open " + file);
    }
}
 
Example 6
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an input stream from a file.
 */
public In(File file) {
    try {
        scanner = new Scanner(file, charsetName);
        scanner.useLocale(usLocale);
    }
    catch (IOException ioe) {
        System.err.println("Could not open " + file);
    }
}
 
Example 7
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an input stream from a URL.
 */
public In(URL url) {
    try {
        URLConnection site = url.openConnection();
        InputStream is     = site.getInputStream();
        scanner            = new Scanner(new BufferedInputStream(is), charsetName);
        scanner.useLocale(usLocale);
    }
    catch (IOException ioe) {
        System.err.println("Could not open " + url);
    }
}
 
Example 8
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an input stream from a socket.
 */
public In(java.net.Socket socket) {
    try {
        InputStream is = socket.getInputStream();
        scanner = new Scanner(new BufferedInputStream(is), charsetName);
        scanner.useLocale(usLocale);
    }
    catch (IOException ioe) {
        System.err.println("Could not open " + socket);
    }
}
 
Example 9
Source File: MTLLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("empty-statement")
@Override
public Object load(AssetInfo info) throws IOException{
    reset();
    
    this.key = info.getKey();
    this.assetManager = info.getManager();
    folderName = info.getKey().getFolder();
    matList = new MaterialList();

    InputStream in = null;
    try {
        in = info.openStream();
        scan = new Scanner(in);
        scan.useLocale(Locale.US);
        
        while (readLine());
    } finally {
        if (in != null){
            in.close();
        }
    }
    
    if (matName != null){
        // still have a material in the vars
        createMaterial();
        resetMaterial();
    }
    
    MaterialList list = matList;

    

    return list;
}
 
Example 10
Source File: DoubleStreamExTest.java    From streamex with Apache License 2.0 5 votes vote down vote up
@Test
public void testProduce() {
    Scanner sc = new Scanner("1.0 2.5 3 -4.6 test");
    sc.useLocale(Locale.ENGLISH);
    assertArrayEquals(new double[] {1, 2.5, 3, -4.6}, scannerDoubles(sc).stream().toArray(), 0.0);
    assertEquals("test", sc.next());
}
 
Example 11
Source File: JavaScannerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenScanString_thenCorrect() throws IOException {
    final String input = "Hello 1 F 3.5";
    final Scanner scanner = new Scanner(input);
    scanner.useLocale(Locale.US);

    assertEquals("Hello", scanner.next());
    assertEquals(1, scanner.nextInt());
    assertEquals(15, scanner.nextInt(16));
    assertEquals(3.5, scanner.nextDouble(), 0.00000001);

    scanner.close();
}
 
Example 12
Source File: In.java    From algs4 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initializes an input stream from a URL.
 *
 * @param  url the URL
 * @throws IllegalArgumentException if cannot open {@code url}
 * @throws IllegalArgumentException if {@code url} is {@code null}
 */
public In(URL url) {
    if (url == null) throw new IllegalArgumentException("url argument is null");
    try {
        URLConnection site = url.openConnection();
        InputStream is     = site.getInputStream();
        scanner            = new Scanner(new BufferedInputStream(is), CHARSET_NAME);
        scanner.useLocale(LOCALE);
    }
    catch (IOException ioe) {
        throw new IllegalArgumentException("Could not open " + url, ioe);
    }
}
 
Example 13
Source File: MTLLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressWarnings("empty-statement")
public Object load(AssetInfo info) throws IOException{
    reset();
    
    this.assetManager = info.getManager();
    folderName = info.getKey().getFolder();
    matList = new MaterialList();

    InputStream in = null;
    try {
        in = info.openStream();
        scan = new Scanner(in);
        scan.useLocale(Locale.US);
        
        while (readLine());
    } finally {
        if (in != null){
            in.close();
        }
    }
    
    if (matName != null){
        // still have a material in the vars
        createMaterial();
        resetMaterial();
    }
    
    MaterialList list = matList;

    

    return list;
}
 
Example 14
Source File: TerminalClient.java    From parity with Apache License 2.0 4 votes vote down vote up
private Scanner scan(String text) {
    Scanner scanner = new Scanner(text);
    scanner.useLocale(LOCALE);

    return scanner;
}
 
Example 15
Source File: AvroRecordConverter.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}
 
Example 16
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Create an input stream from standard input.
 */
public In() {
    scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
    scanner.useLocale(usLocale);
}
 
Example 17
Source File: In.java    From algs4 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initializes an input stream from standard input.
 */
public In() {
    scanner = new Scanner(new BufferedInputStream(System.in), CHARSET_NAME);
    scanner.useLocale(LOCALE);
}
 
Example 18
Source File: In.java    From java_jail with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Create an input stream from standard input.
 */
public In() {
    scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
    scanner.useLocale(usLocale);
}
 
Example 19
Source File: TerminalClient.java    From philadelphia with Apache License 2.0 4 votes vote down vote up
private Scanner scan(String text) {
    Scanner scanner = new Scanner(text);
    scanner.useLocale(LOCALE);

    return scanner;
}
 
Example 20
Source File: AvroRecordConverter.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}