Last active
May 15, 2016 03:07
-
-
Save nfaltermeier/4918fc5bf1a9fcd994626770db29f272 to your computer and use it in GitHub Desktop.
Some code I need my friend to help with. The 1st line of code in main(String[] args) doesn't work but the 2nd does.
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
public static void main(String[] args) | |
{ | |
System.out.println(getNumberAtDigit(4532, 3)); | |
System.out.println(getNumbetAtDigit(4532, 4)); | |
} | |
/** | |
* Digits go from left to right, starting at 1 | |
* | |
* @param source | |
* The number to get the digit from | |
* @param digit | |
* The digit to get | |
* @return The number in the digit'th place in source | |
*/ | |
public static int getNumberAtDigit(int source, int digit) | |
{ | |
if(digit > getSize(source)) | |
throw new IndexOutOfBoundsException( | |
"There aren't " + String.valueOf(digit) + " digits in " + String.valueOf(source) + "."); | |
else | |
{ | |
// Remove digits that come after what we want | |
for(int i = getSize(source); i > digit; i--) | |
source = (int) Math.floor(digit / 10); | |
return source % 10; | |
} | |
} | |
public static int getSize(long d) | |
{ | |
String numberS = String.valueOf(d); | |
return numberS.length(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment