Skip to content

Instantly share code, notes, and snippets.

@luchtech
Last active August 21, 2018 07:08
Show Gist options
  • Save luchtech/c1a8817cbf8155b5265af2cf1a446ee9 to your computer and use it in GitHub Desktop.
Save luchtech/c1a8817cbf8155b5265af2cf1a446ee9 to your computer and use it in GitHub Desktop.
Validator for Inputs from User
import java.util.ArrayList;
import java.util.Vector;
public class Valid {
/*
* This user-defined class is made by James Carlo Luchavez.
* This class is composed of the following Data Validating Methods:
* (a) "isDuplicate" returns the index of the given data from the array - Vector, ArrayList, Normal Array
* (b) "removeExcessSpace" returns a lowercase string with only one space from each word or element
* (c) "firstLetterUp" returns a string with first letters of each word capitalized (for names)
* (d) "toInteger" converts a string value to integer
* (e) "toDouble" converts a string value to double
* (f) ""
*/
// Vector Array isDuplicate
static int isDuplicate(String s, Vector<String> arr) { // String Array
for(String x: arr) {
if(x.equalsIgnoreCase(s)) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(int s, Vector<Integer> arr) { // Integer Array
for(int x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(double s, Vector<Double> arr) { // Double Array
for(double x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(char s, Vector<Character> arr) { // Character Array
for(char x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
// ArrayList isDuplicate
static int isDuplicate(String s, ArrayList<String> arr) { // String Array
for(String x: arr) {
if(x.equalsIgnoreCase(s)) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(int s, ArrayList<Integer> arr) { // Integer Array
for(int x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(double s, ArrayList<Double> arr) { // Double Array
for(double x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
static int isDuplicate(char s, ArrayList<Character> arr) { // Character Array
for(char x: arr) {
if(x == s) {
return arr.indexOf(s);
}
}
return -1;
}
// Normal Array isDuplicate
static int isDuplicate(String s, String arr[]) { // String Array
for (int i = 0; i < arr.length; i++) {
if(arr[i].equals(s))
return i;
}
return -1;
}
static int isDuplicate(int s, int arr[]) { // Integer Array
for (int i = 0; i < arr.length; i++) {
if(arr[i] == s)
return i;
}
return -1;
}
static int isDuplicate(double s, double arr[]) { // Double Array
for (int i = 0; i < arr.length; i++) {
if(arr[i] == s)
return i;
}
return -1;
}
static int isDuplicate(char s, char arr[]) { // Character Array
for (int i = 0; i < arr.length; i++) {
if(arr[i] == s)
return i;
}
return -1;
}
// Excess Whitespace Remover
static String removeExcessSpace(String s){
if(s.equals("")) return s;
if(allSpace(s)) return "";
String hold = "";
char c;
for (int i = 0; i < s.length(); i++) { // REMOVES INAPPROPRIATE WHITESPACES
c = s.charAt(i);
if(Character.isLetter(c)) {
hold+=c;
}
else if(Character.isWhitespace(c) && hold.length() > 0 && !Character.isWhitespace(hold.charAt(hold.length()-1))) {
hold+=" ";
}
}
return hold;
}
// First Letter to Uppercase
static String firstLetterUp(String s) {
s = removeExcessSpace(s);
if(s.equals("")) return s;
String hold = "";
String[] arr = s.split(" ");
for (int i = 0; i < arr.length; i++) { // CONVERTS FIRST LETTER FOR EACH WORD
arr[i] = arr[i].toUpperCase().charAt(0)+arr[i].toLowerCase().substring(1);
hold+=arr[i];
if(i != arr.length-1) {
hold+=" ";
}
}
return hold;
}
// String to Integer
static int toInteger(String s) {
if(s == null) return -1;
String hold = "";
char c;
for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(Character.isDigit(c)) {
hold+=c;
}
if(c == '-' && hold.equals("")){
hold+=c;
}
}
if(hold.equals("")) return 0;
else return Integer.parseInt(hold);
}
// String to Double
static Double toDouble(String s) {
if(s == null) return (double) -1;
String hold = "";
int ctr = 0;
char c;
for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(Character.isDigit(c)) {
hold+=c;
}
else if(c == '-' && hold.equals("")) {
hold+=c;
}
else if(c == '.' && ctr != 1) {
hold+=c;
ctr++;
}
}
if(hold.equals("")) return 0.0;
else return Double.parseDouble(hold);
}
// Checks if a string contains only whitespaces
static boolean allSpace(String s) {
if(s == null) return true;
char c;
for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(!Character.isWhitespace(c)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment