Skip to content

Instantly share code, notes, and snippets.

@nfaltermeier
Last active May 15, 2016 03:07
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 nfaltermeier/4918fc5bf1a9fcd994626770db29f272 to your computer and use it in GitHub Desktop.
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.
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