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

The following examples show how to use java.util.Scanner#hasNextBoolean() . 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: PersistentData.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
public boolean getBool(String key, boolean def) {
  String str = readKeyFile(key);
  Scanner scan = new Scanner(str);
  if (scan.hasNextBoolean()) {
    return scan.nextBoolean();
  } else {
    return def;
  }
}
 
Example 2
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 booelan type
 */
public static boolean isBoolean(String str) {
    if (str == null) {
        return false;
    }
    Scanner s = new Scanner(str.trim());
    if (!s.hasNextBoolean()) {
        return false;
    }
    s.nextBoolean();
    return !s.hasNext();
}
 
Example 3
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse a boolean with a default value.
 *
 * @param str string to parse
 * @param def default boolean value
 * @return parsed boolean
 */
public static Boolean parseBoolean(String str, Boolean def) {
    if (str == null) {
        return def;
    }
    Scanner scnr = new Scanner(str);
    if (!scnr.hasNextBoolean()) {
        return def;
    }
    return scnr.nextBoolean();
}
 
Example 4
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 5
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 6
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 7
Source File: Variable.java    From JOpenShowVar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * Parses a string encoded KRL variable to a JOpenShowVar Variable
     *
     * @param callback the Callback from the robot
     * @return the JOpenShowVar Variable
     * @throws NumberFormatException on parsing error
     */
    public static Variable parseVariable(Callback callback) throws NumberFormatException {
        String variable = callback.getVariableName();
        String value = callback.getStringValue();
        int id = callback.getId();
        long readTime = callback.getReadTime();
//        int option = callback.getOption();

        Scanner sc = new Scanner(value);
        Variable var;
        if (sc.hasNextInt()) {
            var = new Int(id, variable, sc.nextInt(), readTime);
            sc.close();
        } else if (sc.hasNextFloat()) {
            var = new Real(id, variable, (double) sc.nextFloat(), readTime);
            sc.close();
        } else if (sc.hasNextDouble()) {
            var = new Real(id, variable, sc.nextDouble(), readTime);
            sc.close();
        } else if (sc.hasNextBoolean()) {
            var = new Bool(id, variable, sc.nextBoolean(), readTime);
            sc.close();
        }else if (value.contains("#")) {
            var = new Enum(id, variable, sc.nextLine(), readTime);
            sc.close();
        } else if (value.contains("{")) {
            sc.close();
            var = new Struct(id, variable, Struct.parseString(value), readTime);
        } else {
            var = new Real(id, variable, Double.parseDouble(value), readTime);
            sc.close();
        }
        return var;
    }
 
Example 8
Source File: Struct.java    From JOpenShowVar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Parses a String encoded KRL Struct to a JOpenShowVar Struct
 * @param value the KRL String encoded Struct
 * @return a JOpenShowVar Struct 
 */
public static List<StructNode> parseString(String value) {
    if (value.contains(":")) {
        value = value.substring(value.indexOf(":") + 1, value.indexOf("}") + 1);
    } else {
        value = value.substring(value.indexOf("{") + 1, value.indexOf("}") + 1);
        value = " " + value;
    }
    value = value.replaceAll("\\{", "").replaceAll("\\}", "");
    List<StructNode> nodes = new ArrayList<>();
    String[] split1 = value.split(",");
    for (String str1 : split1) {
        String[] split2 = str1.split(" ");
        String name = split2[1];
        Object val;
        Scanner sc = new Scanner(split2[2]);
        if (sc.hasNextInt()) {
            val = sc.nextInt();
        } else if (sc.hasNextDouble()) {
            val = sc.nextDouble();
        } else if (sc.hasNextBoolean()) {
            val = sc.nextBoolean();
        } else {
            val = sc.next();
        }
        StructNode node = new StructNode(name, val);
        nodes.add(node);
    }
    return nodes;
}