Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseCalculator {
- public static void main(String[] args) {
- if(verifyInput(args)) {
- System.out.println(calculateBase(returnBase(args),returnNumber(args)));
- String[] test = returnNumber(args);
- } else {
- System.out.println("Error, Your input could not be validated");
- }
- }
- private static int findBase(String[] args) {
- int size = args.length;
- String base;
- for(int i = 0; i < size; i++) {
- if (args[i].equals("-b")) {
- return i + 1;
- }
- }
- return 0;
- }
- private static int returnBase(String[] args) {
- String fReturn;
- if (findBase(args) < 1) {
- return 2;
- }
- return Integer.parseInt(args[findBase(args)]);
- }
- public static int calculateBase(int b, String[] args) {
- int count = 0;
- int result = 0;
- for(int i = 0; i < args.length; i++) {
- if(!args[i].equals("null")) {
- count++;
- }
- }
- //System.out.print(count);
- for(int i = 0; i < args.length; i++) {
- if(!args[i].equals("null")) {
- result += Integer.parseInt(args[i]) * (Math.pow(b,count-1));
- count--;
- }
- }
- return result;
- }
- private static String[] returnNumber(String[] args) {
- int base = findBase(args);
- String[] result = args;
- if (base > 1) {
- for (int i = base - 1; i < args.length; i++) {
- result[i] = "null";
- }
- } else if(base != 0) {
- result[0] = "null";
- result[1] = "null";
- }
- return args;
- }
- protected static boolean verifyInput(String[] args) {
- if(returnBase(args) < 2) {
- return false;
- }
- if(findBase(args) < args.length - 1 && findBase(args) > 1) {
- return false;
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment