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

The following examples show how to use java.util.Scanner#hasNextLong() . 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: Huawei60.java    From Project with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextLong()) {
        long inputValue = scanner.nextLong();
        if (inputValue < 2) {
            return;
        }
        System.out.println(getResult(inputValue));
    }
}
 
Example 2
Source File: PersistentData.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
public long getJobId() {
  String str = readKeyFile(com.graphicsfuzz.glesworker.Constants.PERSISTENT_KEY_JOB_ID);
  Scanner scan = new Scanner(str);
  if (scan.hasNextLong()) {
    return scan.nextLong();
  } else {
    return -1L;
  }
}
 
Example 3
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param str string to check
 * @return whether the string is a long value
 */
public static boolean isLong(String str) {
    if (str == null) {
        return false;
    }
    Scanner s = new Scanner(str.trim());
    if (!s.hasNextLong()) {
        return false;
    }
    s.nextLong();
    return !s.hasNext();
}
 
Example 4
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse a long with a default value.
 *
 * @param str string to parse
 * @param def default long value
 * @return parsed long
 */
public static Long parseLong(String str, Long def) {
    if (str == null) {
        return def;
    }
    Scanner scnr = new Scanner(str);
    if (!scnr.hasNextLong()) {
        return def;
    }
    return scnr.nextLong();
}
 
Example 5
Source File: DataCubeColumn.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 6
Source File: DataCubeDimension.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 7
Source File: SchemaBuilder.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);

    return sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 8
Source File: NumberTheoryForNewbies.java    From interviews with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	while (input.hasNextLong()) {
		String inputValue = input.nextLine();
		StringBuilder minimal = new StringBuilder();
		StringBuilder maximal = new StringBuilder();
		char[] characters = inputValue.toCharArray();
		int length = characters.length;
		Arrays.sort(characters);
		int index;
		for (index = 0; index < length; index++) {
			if (characters[index] != '0') {
				break;
			}
		}
		if (index != 0) {
			characters[0] = characters[index];
			characters[index] = '0';
		}
		for (int i = 0; i < length; i++) {
			minimal.append(characters[i]);
		}
		Arrays.sort(characters);
		for (int i = length - 1; i > -1; i--) {
			maximal.append(characters[i]);
		}
		long maximalNumber = Long.valueOf(maximal.toString());
		long minimalNumber = Long.valueOf(minimal.toString());
		long difference = maximalNumber - minimalNumber;
		System.out.println(maximal + " - " + minimal + " = " + difference
				+ " = 9 * " + (difference / 9));
	}
}
 
Example 9
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());
        }
    }
}
 
Example 10
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());
        }
    }
}