Skip to content

Instantly share code, notes, and snippets.

@janoulle
Created December 29, 2011 01:37
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 janoulle/1531024 to your computer and use it in GitHub Desktop.
Save janoulle/1531024 to your computer and use it in GitHub Desktop.
Multiplying Digits in a String
public static int parseString(String digits) {
int product = 1;
//Strips out non-word characters
digits = digits.replaceAll("\\W","");
for (int i = 0; i < digits.length(); i++){
product *= Integer.parseInt(digits.substring(i,(i+1)));
}
return (digits.length() > 0) ? product:0;
/*
try {
for (int i = 0; i < digits.length(); i++){
//
product *= Integer.parseInt(digits.substring(i,(i+1)));
}
}
catch (NumberFormatException e) {
System.out.println(e);
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment