2301/what-the-best-way-check-string-represents-an-integer-in-java
I normally use the following idiom to check if a String can be converted to an integer. public boolean isInteger( String input ) { try { Integer.parseInt( input ); return true; } catch( Exception e ) { return false; } } Is it just me, or does this seem a bit hackish? What's a better way?
If you are not concerned with potential overflow problems this function will perform about 20-30 times faster than using Integer.parseInt(). public static boolean isInteger(String str) { if (str == null) { return false; } int length = str.length(); if (length == 0) { return false; } int i = 0; if (str.charAt(0) == '-') { if (length == 1) { return false; } i = 1; } for (; i < length; i++) { char c = str.charAt(i); if (c < '0' || c > '9') { return false; } } return true; }
You can also use regular expression.
str.matches("-?\\d+");
It will give you output as:
-? --> negative sign, could have none or one \\d+ --> one or more digits
What method is the "best" for convert ...READ MORE
You could probably use the Joiner class ...READ MORE
There are two approaches to this: for(int i ...READ MORE
I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE
Check if a string is numeric public class ...READ MORE
Java 8 Lambda Expression is used: String someString ...READ MORE
this problem is solved using streams and ...READ MORE
Use java.lang.String.format() method. String.format("%05d", number ...READ MORE
We can do this in 2 ways: String ...READ MORE
String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.