Guest User

Untitled

a guest
Nov 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. class BaseCalculator {
  2.     public static void main(String[] args) {
  3.         if(verifyInput(args)) {
  4.             System.out.println(calculateBase(returnBase(args),returnNumber(args)));
  5.             String[] test = returnNumber(args);
  6.         } else {
  7.             System.out.println("Error, Your input could not be validated");
  8.         }
  9.     }
  10.    
  11.     private static int findBase(String[] args) {
  12.         int size = args.length;
  13.         String base;
  14.         for(int i = 0; i < size; i++) {
  15.             if (args[i].equals("-b")) {
  16.                 return i + 1;
  17.             }
  18.         }
  19.         return 0;
  20.     }
  21.     private static int returnBase(String[] args) {
  22.         String fReturn;
  23.         if (findBase(args) < 1) {
  24.             return 2;
  25.         }
  26.         return Integer.parseInt(args[findBase(args)]);
  27.     }
  28.    
  29.     public static int calculateBase(int b, String[] args) {
  30.         int count = 0;
  31.         int result = 0;
  32.         for(int i = 0; i < args.length; i++) {
  33.             if(!args[i].equals("null")) {
  34.                 count++;
  35.             }
  36.         }
  37.         //System.out.print(count);
  38.         for(int i = 0; i < args.length; i++) {
  39.             if(!args[i].equals("null")) {
  40.                 result += Integer.parseInt(args[i]) * (Math.pow(b,count-1));
  41.                 count--;
  42.             }
  43.         }
  44.         return result;
  45.     }
  46.    
  47.     private static String[] returnNumber(String[] args) {
  48.         int base = findBase(args);
  49.         String[] result = args;
  50.         if (base > 1) {
  51.             for (int i = base - 1; i < args.length; i++) {
  52.                 result[i] = "null";
  53.             }
  54.         } else if(base != 0) {
  55.             result[0] = "null";
  56.             result[1] = "null";
  57.         }
  58.         return args;
  59.        
  60.     }
  61.  
  62.     protected static boolean verifyInput(String[] args) {
  63.         if(returnBase(args) < 2) {
  64.             return false;
  65.         }
  66.         if(findBase(args) < args.length - 1 && findBase(args) > 1) {
  67.             return false;
  68.         }
  69.         return true;
  70.     }
  71. }
Add Comment
Please, Sign In to add comment