Java Code Examples for java.lang.Integer#parseInt()

The following examples show how to use java.lang.Integer#parseInt() . These examples are extracted from open source projects. 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
@Override
public void renderHTML(PrintWriter writer, Map<String, String> params) {
  int brokerId = Integer.parseInt(params.get("brokerid"));
  String clusterName = params.get("cluster");
  printHeader(writer);

  writer.print("<div> <p><a href=\"/\">Home</a> > "
 + "<a href=\"/servlet/clusterinfo?name=" + clusterName + "\"> " + clusterName
 + "</a> > broker " + brokerId + "</p> </div>");

  writer.print("<table class=\"table table-hover\"> ");
  writer.print("<th class=\"active\"> Timestamp </th> ");
  writer.print("<th class=\"active\"> Stats </th>");
  writer.print("<tbody>");

  try {
    generateBrokerHtml(writer, clusterName, brokerId);
    writer.print("</tbody></table>");
    writer.print("</td> </tr>");
    writer.print("</tbody> </table>");
  } catch (Exception e) {
    LOG.error("Unexpected exception : ", e);
    e.printStackTrace(writer);
  }
  printFooter(writer);
}
 
Example 2
/**
 * Parses a host address from a hostId string (the numeric part after
 * optional non-numeric part).
 * @param hostId The id to parse the address from
 * @return The address
 * @throws SimError if no address could be parsed from the id
 */
private int getHostAddress(String hostId) {
	String addressPart = "";
	if (hostId.matches("^\\d+$")) {
		addressPart = hostId; // host id is only the address
	}
	else if (hostId.matches("^\\D+\\d+$")) {
		String [] parts = hostId.split("\\D");
		addressPart = parts[parts.length-1]; // last occurence is the addr
	}
	else {
		throw new SimError("Invalid host ID '" + hostId + "'");
	}

	return Integer.parseInt(addressPart);
}
 
Example 3
public static void main(String[] args) throws Exception {
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
       "-XX:+UnlockDiagnosticVMOptions",
       "-XX:SharedArchiveFile=./SpaceUtilizationCheck.jsa",
       "-Xshare:dump");

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    String stdout = output.getStdout();
    ArrayList<String> utilization = findUtilization(stdout);

    if (utilization.size() != NUMBER_OF_CHECKED_SHARED_REGIONS )
        throw new RuntimeException("The output format of sharing summary has changed");

    for(String str : utilization) {
        int value = Integer.parseInt(str);
        if (value < MIN_UTILIZATION) {
            System.out.println(stdout);
            throw new RuntimeException("Utilization for one of the regions" +
                "is below a threshold of " + MIN_UTILIZATION + "%");
        }
    }
}
 
Example 4
private void parseIPv6(String name) throws IOException {

        int slashNdx = name.indexOf('/');
        if (slashNdx == -1) {
            address = InetAddress.getByName(name).getAddress();
        } else {
            address = new byte[32];
            byte[] base = InetAddress.getByName
                (name.substring(0, slashNdx)).getAddress();
            System.arraycopy(base, 0, address, 0, 16);

            // append a mask corresponding to the num of prefix bits specified
            int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
            if (prefixLen < 0 || prefixLen > 128) {
                throw new IOException("IPv6Address prefix length (" +
                        prefixLen + ") in out of valid range [0,128]");
            }

            // create new bit array initialized to zeros
            BitArray bitArray = new BitArray(MASKSIZE * 8);

            // set all most significant bits up to prefix length
            for (int i = 0; i < prefixLen; i++)
                bitArray.set(i, true);
            byte[] maskArray = bitArray.toByteArray();

            // copy mask bytes into mask portion of address
            for (int i = 0; i < MASKSIZE; i++)
                address[MASKSIZE+i] = maskArray[i];
        }
    }
 
Example 5
public static TestClassBundledGCM unbundle(Bundle bundle, Gson gson) {
    return new AutoValue_TestClassBundledGCM(
            bundle,
            Byte.parseByte(bundle.getString("some_byte")),
            Boolean.parseBoolean(bundle.getString("some_boolean")),
            Short.parseShort(bundle.getString("some_short")),
            Integer.parseInt(bundle.getString("some_int")),
            Long.parseLong(bundle.getString("some_long")),
            bundle.getString("some_char").charAt(0),
            Float.parseFloat(bundle.getString("some_float")),
            Double.parseDouble(bundle.getString("some_double")),
            bundle.getString("some_string"),
            bundle.getString("some_char_sequence"),
            gson.fromJson(bundle.getString("some_parcelable"), Parcelable.class),
            gson.fromJson(bundle.getString("some_parcelable_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<Parcelable>>(){}.getType()),
            gson.fromJson(bundle.getString("some_parcelable_sparse_array"), new com.google.common.reflect.TypeToken<android.util.SparseArray<Parcelable>>(){}.getType()),
            gson.fromJson(bundle.getString("some_serializable"), Serializable.class),
            gson.fromJson(bundle.getString("some_integer_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<Integer>>(){}.getType()),
            gson.fromJson(bundle.getString("some_string_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<String>>(){}.getType()),
            gson.fromJson(bundle.getString("some_char_sequence_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<CharSequence>>(){}.getType()),
            toPrimitive(gson.fromJson(bundle.getString("some_byte_array"), Byte[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_short_array"), Short[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_char_array"), Character[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_float_array"), Float[].class)),
            gson.fromJson(bundle.getString("some_unknown_object"), new com.google.common.reflect.TypeToken<UnknownObject>(){}.getType()),
            gson.fromJson(bundle.getString("some_unknown_object_list"), new com.google.common.reflect.TypeToken<ArrayList<UnknownObject>>(){}.getType()),
            gson.fromJson(bundle.getString("test_enum"), new com.google.common.reflect.TypeToken<TestEnum>(){}.getType()));
}
 
Example 6
private void parseIPv6(String name) throws IOException {

        int slashNdx = name.indexOf('/');
        if (slashNdx == -1) {
            address = InetAddress.getByName(name).getAddress();
        } else {
            address = new byte[32];
            byte[] base = InetAddress.getByName
                (name.substring(0, slashNdx)).getAddress();
            System.arraycopy(base, 0, address, 0, 16);

            // append a mask corresponding to the num of prefix bits specified
            int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
            if (prefixLen > 128)
                throw new IOException("IPv6Address prefix is longer than 128");

            // create new bit array initialized to zeros
            BitArray bitArray = new BitArray(MASKSIZE * 8);

            // set all most significant bits up to prefix length
            for (int i = 0; i < prefixLen; i++)
                bitArray.set(i, true);
            byte[] maskArray = bitArray.toByteArray();

            // copy mask bytes into mask portion of address
            for (int i = 0; i < MASKSIZE; i++)
                address[MASKSIZE+i] = maskArray[i];
        }
    }
 
Example 7
private void parseIPv6(String name) throws IOException {

        int slashNdx = name.indexOf('/');
        if (slashNdx == -1) {
            address = InetAddress.getByName(name).getAddress();
        } else {
            address = new byte[32];
            byte[] base = InetAddress.getByName
                (name.substring(0, slashNdx)).getAddress();
            System.arraycopy(base, 0, address, 0, 16);

            // append a mask corresponding to the num of prefix bits specified
            int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
            if (prefixLen < 0 || prefixLen > 128) {
                throw new IOException("IPv6Address prefix length (" +
                        prefixLen + ") in out of valid range [0,128]");
            }

            // create new bit array initialized to zeros
            BitArray bitArray = new BitArray(MASKSIZE * 8);

            // set all most significant bits up to prefix length
            for (int i = 0; i < prefixLen; i++)
                bitArray.set(i, true);
            byte[] maskArray = bitArray.toByteArray();

            // copy mask bytes into mask portion of address
            for (int i = 0; i < MASKSIZE; i++)
                address[MASKSIZE+i] = maskArray[i];
        }
    }
 
Example 8
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
    AttributeSet eAttr = getElement().getAttributes();
    attr = sheet.getViewAttributes(this);

    alignment = StyleConstants.ALIGN_CENTER;
    size = 0;
    noshade = null;
    widthValue = null;

    if (attr != null) {
        // getAlignment() returns ALIGN_LEFT by default, and HR should
        // use ALIGN_CENTER by default, so we check if the alignment
        // attribute is actually defined
        if (attr.getAttribute(StyleConstants.Alignment) != null) {
            alignment = StyleConstants.getAlignment(attr);
        }

        noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
        Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
        if (value != null && (value instanceof String)) {
            try {
                size = Integer.parseInt((String)value);
            } catch (NumberFormatException e) {
                size = 1;
            }
        }
        value = attr.getAttribute(CSS.Attribute.WIDTH);
        if (value != null && (value instanceof CSS.LengthValue)) {
            widthValue = (CSS.LengthValue)value;
        }
        topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
        bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
        leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
        rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
    }
    else {
        topMargin = bottomMargin = leftMargin = rightMargin = 0;
    }
    size = Math.max(2, size);
}
 
Example 9
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
    AttributeSet eAttr = getElement().getAttributes();
    attr = sheet.getViewAttributes(this);

    alignment = StyleConstants.ALIGN_CENTER;
    size = 0;
    noshade = null;
    widthValue = null;

    if (attr != null) {
        // getAlignment() returns ALIGN_LEFT by default, and HR should
        // use ALIGN_CENTER by default, so we check if the alignment
        // attribute is actually defined
        if (attr.getAttribute(StyleConstants.Alignment) != null) {
            alignment = StyleConstants.getAlignment(attr);
        }

        noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
        Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
        if (value != null && (value instanceof String)) {
            try {
                size = Integer.parseInt((String)value);
            } catch (NumberFormatException e) {
                size = 1;
            }
        }
        value = attr.getAttribute(CSS.Attribute.WIDTH);
        if (value != null && (value instanceof CSS.LengthValue)) {
            widthValue = (CSS.LengthValue)value;
        }
        topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
        bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
        leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
        rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
    }
    else {
        topMargin = bottomMargin = leftMargin = rightMargin = 0;
    }
    size = Math.max(2, size);
}
 
Example 10
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
    AttributeSet eAttr = getElement().getAttributes();
    attr = sheet.getViewAttributes(this);

    alignment = StyleConstants.ALIGN_CENTER;
    size = 0;
    noshade = null;
    widthValue = null;

    if (attr != null) {
        // getAlignment() returns ALIGN_LEFT by default, and HR should
        // use ALIGN_CENTER by default, so we check if the alignment
        // attribute is actually defined
        if (attr.getAttribute(StyleConstants.Alignment) != null) {
            alignment = StyleConstants.getAlignment(attr);
        }

        noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
        Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
        if (value != null && (value instanceof String)) {
            try {
                size = Integer.parseInt((String)value);
            } catch (NumberFormatException e) {
                size = 1;
            }
        }
        value = attr.getAttribute(CSS.Attribute.WIDTH);
        if (value != null && (value instanceof CSS.LengthValue)) {
            widthValue = (CSS.LengthValue)value;
        }
        topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
        bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
        leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
        rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
    }
    else {
        topMargin = bottomMargin = leftMargin = rightMargin = 0;
    }
    size = Math.max(2, size);
}
 
Example 11
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
    AttributeSet eAttr = getElement().getAttributes();
    attr = sheet.getViewAttributes(this);

    alignment = StyleConstants.ALIGN_CENTER;
    size = 0;
    noshade = null;
    widthValue = null;

    if (attr != null) {
        // getAlignment() returns ALIGN_LEFT by default, and HR should
        // use ALIGN_CENTER by default, so we check if the alignment
        // attribute is actually defined
        if (attr.getAttribute(StyleConstants.Alignment) != null) {
            alignment = StyleConstants.getAlignment(attr);
        }

        noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
        Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
        if (value != null && (value instanceof String)) {
            try {
                size = Integer.parseInt((String)value);
            } catch (NumberFormatException e) {
                size = 1;
            }
        }
        value = attr.getAttribute(CSS.Attribute.WIDTH);
        if (value != null && (value instanceof CSS.LengthValue)) {
            widthValue = (CSS.LengthValue)value;
        }
        topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
        bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
        leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
        rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
    }
    else {
        topMargin = bottomMargin = leftMargin = rightMargin = 0;
    }
    size = Math.max(2, size);
}
 
Example 12
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
    AttributeSet eAttr = getElement().getAttributes();
    attr = sheet.getViewAttributes(this);

    alignment = StyleConstants.ALIGN_CENTER;
    size = 0;
    noshade = null;
    widthValue = null;

    if (attr != null) {
        // getAlignment() returns ALIGN_LEFT by default, and HR should
        // use ALIGN_CENTER by default, so we check if the alignment
        // attribute is actually defined
        if (attr.getAttribute(StyleConstants.Alignment) != null) {
            alignment = StyleConstants.getAlignment(attr);
        }

        noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
        Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
        if (value != null && (value instanceof String)) {
            try {
                size = Integer.parseInt((String)value);
            } catch (NumberFormatException e) {
                size = 1;
            }
        }
        value = attr.getAttribute(CSS.Attribute.WIDTH);
        if (value != null && (value instanceof CSS.LengthValue)) {
            widthValue = (CSS.LengthValue)value;
        }
        topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
        bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
        leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
        rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
    }
    else {
        topMargin = bottomMargin = leftMargin = rightMargin = 0;
    }
    size = Math.max(2, size);
}
 
Example 13
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 14
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 15
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 16
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 17
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 18
Source Project: JDKSourceCode1.8   File: AbstractPreferences.java    License: MIT License 3 votes vote down vote up
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 19
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 20
/**
 * Implements the <tt>getInt</tt> method as per the specification in
 * {@link Preferences#getInt(String,int)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>int</tt> with
 * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as an int.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as an int.
 * @return the int value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         an int.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public int getInt(String key, int def) {
    int result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}