Created
May 27, 2018 22:55
-
-
Save DarkCoder28/4e56adaec1c870afed40b25903f35bf6 to your computer and use it in GitHub Desktop.
Basic integer and double parser with error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aidensheeran.prg10EmployeeClass; | |
import com.aidensheeran.prg10EmployeeClass.GUI.Sign; | |
@SuppressWarnings("null") | |
public class Parse { | |
public static boolean isDouble(String string) { | |
string = string.trim(); | |
try { | |
Double.parseDouble(string); | |
} catch(NumberFormatException error) { | |
return false; | |
} | |
return true; | |
} | |
public static boolean isInt(String string) { | |
string = string.trim(); | |
if (string.contains(".")) { GUI.out("Input must be an integer!", Sign.CAUTION); return false; } | |
try { | |
Integer.parseInt(string); | |
} catch(NumberFormatException error) { | |
return false; | |
} | |
return true; | |
} | |
public static double doubles(String string) { | |
double out = 0; | |
string = string.trim(); | |
try { | |
out = Double.parseDouble(string); | |
} catch(NumberFormatException error) { | |
GUI.out("ERROR: " + error.getMessage(), Sign.CAUTION); | |
return (Double) null; | |
} | |
return out; | |
} | |
public static int ints(String string) { | |
int out = 0; | |
string = string.trim(); | |
if (string.contains(".")) { GUI.out("Input must be an integer!", Sign.CAUTION); return out; } | |
try { | |
out = Integer.parseInt(string); | |
} catch(NumberFormatException error) { | |
GUI.out("ERROR: " + error.getMessage(), Sign.CAUTION); | |
return (Integer) null; | |
} | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment