Java Scanner Usage getInteger(String parameter)

Here you can find the source of getInteger(String parameter)

Description

Get the integer value from a string.

License

Open Source License

Parameter

Parameter Description
parameter Text to extract the integer.

Return

The integer value from the string.

Declaration

public static int getInteger(String parameter) 

Method Source Code

//package com.java2s;
/**//from  www  .j  a v a2 s  . c om
 * Copyright (c) 2006, 2009 Hugo Corbucci and others.<br>
 * All rights reserved. This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html<br>
 * <br>
 * Contributors:<br>
 * Cristiane M. Sato - initial API and implementation<br>
 * Julien Renaut, Mariana V. Bravo, Luiz C. Real, Jonas K. Hirata - later contributions<br>
 * <br>
 * This file was created on 2006/04/03, 10:44:46, by Cristiane M. Sato.<br>
 * It is part of package br.org.archimedes on the br.org.archimedes.core project.<br>
 */

import java.util.Locale;
import java.util.Scanner;

public class Main {
    /**
     * Get the integer value from a string.
     * 
     * @param parameter
     *            Text to extract the integer.
     * @return The integer value from the string.
     */
    public static int getInteger(String parameter) {

        String withDots = withDot(parameter);
        Scanner stringScanner = new Scanner(withDots);
        stringScanner.useLocale(Locale.US);

        return stringScanner.nextInt();
    }

    /**
     * Replaces commas by dots in the decimal representation.
     * 
     * @param stringWithComma
     *            The string to modify.
     * @return Returns the new string.
     */
    private static String withDot(String stringWithComma) {

        String returnValue;
        if (stringWithComma == null) {
            returnValue = null;
        } else {
            returnValue = stringWithComma.replace(',', '.');
        }
        return returnValue;
    }
}

Related

  1. getExtensions(Scanner scanner)
  2. getFirstInt(String s)
  3. getInput(Scanner in, String message, boolean allowNull)
  4. getInt()
  5. getInt(Scanner sc)
  6. getLines(String str)
  7. getLineStartingWith(Scanner scanner, String starts)
  8. getLngIn()
  9. getNextInput(Scanner scan)