Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active March 6, 2017 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/3fc8569710ebb592e0597e079f937b47 to your computer and use it in GitHub Desktop.
Save kamontat/3fc8569710ebb592e0597e079f937b47 to your computer and use it in GitHub Desktop.
Check Number in TextField
/**
* The checker to check string can or can't change to number.
*
* @author kamontat
* @version 1.0
* @since Mon 06/Mar/2017 - 7:39 PM
*/
public class Checker {
/**
* check input string must contains digit and <b>dot</b> ONLY.
*
* @param input
* string to check.
* @return true if input is {@link Float} or {@link Double}.
*/
public static boolean isAllDecimal(String input) {
boolean dot = false;
// if input is empty String
if (input == null || input.length() == 0) return false;
// check every char in input String
for (char aChar : input.toCharArray()) {
// check 'dot' first time
if (aChar == '.' && !dot) dot = true;
// else have 'dot' more than 1
else if (aChar == '.') return false;
// else isn't 'dot'
else if (!Character.isDigit(aChar)) return false;
}
return true;
}
/**
* check input string must contains digit ONLY.
*
* @param input
* string to check.
* @return true if input is {@link Integer} or {@link Long}.
*/
public static boolean isAllNumber(String input) {
// if input is empty String
if (input == null || input.length() == 0) return false;
// check every char in input String
for (char aChar : input.toCharArray()) {
if (!Character.isDigit(aChar)) return false;
}
return true;
}
/**
* for testing only.
*
* @param args
* no used.
*/
public static void main(String[] args) {
System.out.println(isAllDecimal("120.22")); // true
System.out.println(isAllDecimal("120.23.1")); // false
System.out.println(isAllDecimal("1214")); // true
System.out.println(isAllDecimal("a554.4")); // false
System.out.println(isAllDecimal("879.65755")); // true
System.out.println("-----------------------------------");
System.out.println(isAllNumber("12455")); // true
System.out.println(isAllNumber("120a24a")); // false
System.out.println(isAllNumber("970766")); // true
System.out.println(isAllNumber("12.44")); // false
System.out.println(isAllNumber("0999")); // true
}
}
private void warn() {
if (isAllNumberIn(textField.getText())) {
// have only Integer or Double. :)
} else {
// have some String too. :(
}
}
private Boolean isAllNumberIn(String input) {
boolean checkDot = false;
// if input is empty String
if (input.length() == 0) return false;
// check every char in input String
for (int i = 0; i < input.length(); i++) {
char aChar = input.charAt(i);
// check 'dot' first time
if (aChar == '.' && !checkDot) checkDot = true;
// else have 'dot' more than 1
else if (aChar == '.') return false;
// else isn't 'dot'
else if (!Character.isDigit(aChar)) return false;
}
return true;
}
import com.sun.istack.internal.Nullable;
/**
* This class contains 3 useful method: <br>
* <ol>
* <li>{@link #parseTo(String, Class)} - parse <code>input</code> to Number in <code>aClass</code> Class.</li>
* <li>{@link #getError()} - get error message.</li>
* <li>{@link #isError()} - check is error occurred.</li>
* </ol>
*
* @author kamontat
* @version 1.0
* @since Mon 06/Mar/2017 - 11:55 PM
*/
public class Parser {
private static final String COMPLETE = "No Error";
private String errorMessage;
/**
* to create this class must used this method to get parsable
*
* @return Parser to use {@link Parser} method
*/
public static Parser getParsable() {
return new Parser();
}
/**
* Using Method {@link #parse(String, Class)} with saving any error to error message so you can get error message in {@link #getError()} method.
*
* @param input
* String input to parse
* @param tClass
* class that want to cast to (cannot be interface or anonymous class).
* @param <T>
* expected output class.
* @return number of input in <code>{@link Class}</code> class, or null if cannot parse to <code>Class</code>.
*/
public <T extends Number> T parseTo(String input, Class<T> tClass) {
try {
return parse(input, tClass);
} catch (Exception e) {
errorMessage = e.getMessage();
}
return null;
}
/**
* check is error occurred.
*
* @return true if have error occurred; otherwise, false
*/
public boolean isError() {
return !errorMessage.equals(COMPLETE);
}
/**
* given error message if have error or {@link #COMPLETE}="{@value #COMPLETE}".
*
* @return error message or {@value #COMPLETE}
*/
public String getError() {
return errorMessage;
}
/**
* constructor, just set error to COMPLETE
*/
private Parser() {
this.errorMessage = COMPLETE;
}
/**
* parse input to aClass. <br>
* Example: <br>
* <code>parse("12.34", Double.class);</code> <br>
* <code>parse("2", Integer.class);</code>
*
* @param input
* input to parse.
* @param aClass
* class that want to cast to (cannot be interface or anonymous class).
* @param <T>
* expected output class.
* @return number of input in <code>{@link Class}</code> class.
* @throws NumberFormatException
* if can't parse input to aClass, input is null or empty.
* @throws ClassCastException
* if can't cast input to aClass, the aClass is interface or anonymous.
*/
@SuppressWarnings("unchecked")
private <T extends Number> T parse(@Nullable String input, Class<T> aClass) throws NumberFormatException, ClassCastException {
if (input == null || input.equals("")) throw new NumberFormatException("input string is null or empty.");
if (aClass.isInterface() || aClass.isAnonymousClass())
throw new ClassCastException("cannot cast to interface or anonymous class.");
if (aClass.getName().equals(Byte.class.getName())) {
return aClass.cast(new Byte(input));
} else if (aClass.getName().equals(Double.class.getName())) {
return aClass.cast(new Double(input));
} else if (aClass.getName().equals(Float.class.getName())) {
return aClass.cast(new Float(input));
} else if (aClass.getName().equals(Integer.class.getName())) {
return aClass.cast(new Integer(input));
} else if (aClass.getName().equals(Long.class.getName())) {
return aClass.cast(new Long(input));
} else if (aClass.getName().equals(Short.class.getName())) {
return aClass.cast(new Short(input));
}
throw new ClassCastException("The class isn't Number, or Not implement yet.");
}
/**
* For test only
*
* @param args
* no used
*/
public static void main(String[] args) {
System.out.println(Parser.getParsable().parseTo("2120e22", Double.class) != null); // true
System.out.println(Parser.getParsable().parseTo("120.2.2", Double.class) != null); // false
System.out.println(Parser.getParsable().parseTo("43.22", Double.class) != null); // true
System.out.println(Parser.getParsable().parseTo("128", Byte.class) != null); // false
System.out.println(Parser.getParsable().parseTo("584958497.134965866958", Float.class) != null); // true
System.out.println(Parser.getParsable().parseTo("2e10", Integer.class) != null); // false
System.out.println(Parser.getParsable().parseTo("223410", Integer.class) != null); // true
System.out.println(Parser.getParsable().parseTo("abcd", Number.class) != null); // false
System.out.println(Parser.getParsable().parseTo("-32768", Short.class) != null); // true
}
}
@kamontat
Copy link
Author

kamontat commented Mar 6, 2017

  • Parser is class to parse string to number.
  • methodChecking is method to check number with only 1 dot.
  • Checker is class to check string is number or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment