samedi 30 avril 2016

JAVA How to get the summation of 14-based values?

Currently, I am trying to write a program where it would ask the user for a 14-based number and it would shoot out a 14-based number. The prompt is as follows... "An alien species uses 14-based numbering system. Their ten digits, 0 through 9, are the same as our decimal system. They use A, J, Q and K to represent decimal 10, 11, 12 and 13, respectively. They hire you to write a Java program to do the summation of their two numbers. The program should prompt users for two 14-based numbers and then display the summation of those two numbers. The output should also be 14-based. (They are unwilling to learn our decimal systems!) For example, if inputs are 17 and 96, the output should be AK."

when i enter 17 and 96, it shoots out AK, which is what i wanted. When i enter something like 1Z, it would pop up the "Your first/second input is invalid," which is also what is expected. But When i input something like 1A, or j1, it would give me the same error " Your first/second input is invalid" , though it should go through. I feel like i did something wrong in the validateinput method, but i am not quite sure. Any help would be greatly appreciated. Thanks! Thanks,

enter code here

import java.util.Scanner;

public class H5_hieu {

   public static void main(String[] args)
   {
       System.out.println("Please enter a 14 base value: ");
       Scanner input = new Scanner(System.in);
       String first = input.nextLine(); 
       String second = input.nextLine();
       boolean isFirstValid = validateInputs(first);
       boolean isSecondValid = validateInputs(second);
       while (!isFirstValid || !isSecondValid){
          if (!isFirstValid){ 
            System.out.println("Your first input is invalid");
          }
          if (!isSecondValid){
            System.out.println("Your second input is invalid");
          }
          System.out.println("Please enter a 14 base value: ");

          first = input.nextLine();
          second = input.nextLine();
          isFirstValid = validateInputs(first);
          isSecondValid = validateInputs(second);
       }

       int firstInDecimal = convertFrom14To10(first.toUpperCase());
       int secondInDecimal = convertFrom14To10(second.toUpperCase());
      System.out.println(convertFrom10To14( firstInDecimal + secondInDecimal));




   }

   public static boolean validateInputs(String input) {
      for ( int i = 0;i < input.length(); i++){
         char currentChar = input.charAt(i);
         if (!Character.isDigit(currentChar) && (currentChar != 'A' || currentChar != 'J' || currentChar != 'Q' || currentChar != 'K' || currentChar != 'a' || currentChar != 'j' || currentChar != 'q' || currentChar != 'k')) {
            return false;
         }
      }
      return true;
   }



   public static String convertFrom10To14(int input){
      boolean done = false;
      int quotient = input;
      int remainder = 0;
      String result = "";
      while (!done) {
         remainder = quotient % 14;

         quotient = quotient / 14;


         //System.out.println("quotient: " + quotient + " remainder: " + convertDigit(remainder));
         result = convertDigit(remainder) + result ;

         if (quotient == 0)
            done = true;
      }
      return result;
  }

   public static int convertFrom14To10(String input) {

      int length = input.length();
      int result = 0;

      for(int i = 0; i < length; i++){

         char currentChar = input.charAt(i);
         //System.out.println("Character at index " + i + " : " + currentChar);
         int valueOfChar = getCoeff(currentChar);
       // System.out.println("Decimal value of currentChar: " + valueOfChar);
         int power = length - 1 - i;
         //System.out.println("Power: " + power);
         result = result + (valueOfChar * (int)Math.pow(14, power));
        //System.out.println();
      }
     // System.out.println("Decimal of " + input + " is: " + result + "\n");   
      return result;
   }

   public static int getCoeff(char character) {
      if (character == 'A'){
         return 10;
      } else if (character == 'J'){
         return 11;
      } else if (character == 'Q'){
          return 12;
      } else if (character == 'K'){
          return 13;
      } else {
          return Integer.parseInt(String.valueOf(character));
      }

    }
    public static String convertDigit( int number)
    {
    if (number == 10){
      return "A";
    } else if ( number == 11){
         return "J";
      } else if (number == 12){
           return "Q";
        } else if (number == 13){
            return "K";
          } else {
               return String.valueOf(number);
               }
    }


  }

Aucun commentaire:

Enregistrer un commentaire