Skip to content

Instantly share code, notes, and snippets.

@mkmozgawa
Created May 16, 2017 20:00
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 mkmozgawa/daa49c4546a637be757198f884d41692 to your computer and use it in GitHub Desktop.
Save mkmozgawa/daa49c4546a637be757198f884d41692 to your computer and use it in GitHub Desktop.
package consoleConverter;
class BaseOutOfBoundException extends Exception {
public String getMessage() {
return ("Base out of bound");
}
}
class NumberUninterpretableException extends Exception {
public String getMessage() {
return ("Number uninterpretable");
}
}
public class Converter {
private String number;
private int baseFrom;
private int baseTo;
public Converter(String number, int baseFrom, int baseTo) {
this.number = number;
this.baseFrom = baseFrom;
this.baseTo = baseTo;
}
public String convertBase() throws BaseOutOfBoundException, NumberUninterpretableException
{
if (2 > this.baseFrom || this.baseFrom > 16 || 2 > this.baseTo || this.baseTo > 16) {
throw new BaseOutOfBoundException();
}
try {
return Integer.toString(Integer.parseInt(this.number, this.baseFrom),this.baseTo);
} catch (NumberFormatException e) {
throw new NumberUninterpretableException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment