Is the following Code correct, for calculating Decimal to Triskaidecimal and Triskaidecimal to Decimal, I hope this code is not right

import java.util.Scanner;
public class converter
{
    /* 1) Decimal To Triskaidecimal
        2) Triskaidecimal To Decimal
        3)Exit.
    */
        public static void main(String ar[])
        {
            int a, b;
            String s;
            System.out.println("Please Select:\n1)Decimal To Triskaidecimal.\n2)Tridecimal To Decimal.\n3)Exit.");
            Scanner input = new Scanner(System.in);
            s = input.nextLine();
            System.out.println("You have Selected:" +s);
            switch(s)
            {
                case "1":
                System.out.println("Decimal To Triskaidecimal Conversion:");
                Scanner ip_Dec2Tri = new Scanner(System.in);
                //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Please Enter A Decimal Number To Convert to Triskaidecimal Number:");
                a = ip_Dec2Tri.nextInt();
                //String Dec_Hex = br.readLine();
                int triskaidecimal = (a%13);
                if (triskaidecimal==10)
                {
                    System.out.println("The Decimal To Triskaidecimal is : A");
                }
                else if (triskaidecimal==11)
                {
                    System.out.println("The Decimal To TriskaiDecimal is : B");
                }
                else if (triskaidecimal ==12)
                {
                    System.out.println("The Decimal To TriskaiDecimal is : C");
                }
                else
                {
                    System.out.println("The Decimal To Triskaidecimal is :"+triskaidecimal);
                }

                break;
                case "2":
                System.out.println("Triskaidecimal To Decimal Conversion:");
                System.out.println("Please Enter a Triskaidecimal Number to Convert it into Decimal:");
                Scanner ip_Tri2Dec = new Scanner(System.in);
                b = ip_Tri2Dec.nextInt();
                int decimal = (b*13);
                System.out.println("The Tridecimal To Decimal is : " +decimal);
                break;
                case "3":
                System.out.println("System is Quitting!!!\nThank you for using the converter\n");
                System.exit(0);
                default:
                System.out.println("Please Provide the Correct Input\nTry again!");
                System.exit(1);
            }//end of switch s



        }
}

Dani AI

Generated

The code in the first post only takes a single remainder and prints it, and it reads triskaidecimal (base‑13) input with nextInt() so letters like A/B/C cause an exception. As noted, base‑13 strings must be read as Strings and processed character by character. pointed to external references on base conversion; the algorithm below follows that standard approach.

Algorithm summary:

  • Decimal → base‑13: handle zero and sign, repeatedly divide by 13, map remainders 0–9 to '0'–'9' and 10→'A', 11→'B', 12→'C', collect remainders and reverse the result.
  • Base‑13 → decimal: trim and handle optional sign, iterate left to right, map each char to its numeric value (accept upper/lower case), and do acc = acc*13 + digit. Reject any invalid characters.
    Examples (for clarity): decimal 13 → "10", decimal 12 → "C", base‑13 "13A" → decimal 218.

Practical Java helpers (concise, robust for typical inputs):

static String toBase13(long n) {
    if (n == 0) return "0";
    boolean neg = n < 0;
    long x = neg ? -n : n;
    StringBuilder sb = new StringBuilder();
    while (x > 0) {
        int r = (int)(x % 13);
        sb.append(r < 10 ? (char)('0' + r) : (char)('A' + (r - 10)));
        x /= 13;
    }
    if (neg) sb.append('-');
    return sb.reverse().toString();
}

static long fromBase13(String s) {
    if (s == null) throw new IllegalArgumentException("null");
    s = s.trim();
    if (s.isEmpty()) throw new IllegalArgumentException("empty");
    int i = 0; boolean neg = false;
    if (s.charAt(0) == '-') { neg = true; i = 1; }
    long acc = 0;
    for (; i < s.length(); i++) {
        char c = s.charAt(i);
        int d;
        if (c >= '0' && c <= '9') d = c - '0';
        else if (c >= 'A' && c <= 'C') d = 10 + (c - 'A');
        else if (c >= 'a' && c <= 'c') d = 10 + (c - 'a');
        else throw new IllegalArgumentException("invalid digit: " + c);
        acc = acc * 13 + d;
    }
    return neg ? -acc : acc;
}

Troubleshooting notes: always read base‑13 input as a String, call trim() and toUpperCase() or accept both cases, test edge cases (0, negatives, invalid chars). For very large values use BigInteger instead of long to avoid overflow.

Recommended Answers

All 5 Replies

You can tell for yourself if it's right or not by testing it

Why do you hope it's not right?

I'd tested it, it only works for numbers, say for example if I want to convert a decimal to triskaideciman(base13) it works good for only numbers, say for example I want to convert 13A to decimal it is giving me an error message

You read the input for that as an int, and ints do not contain As Bs or Cs, so you get an error. You have to read that input as a String and deal with each character in turn - basically the reverse of the code you have for int -> 13.

ps Your code for int to base 13 doesn't seem to have a any logic for values >= 13


How to define a logic for converting a decimal number into triskaidecimal ? I've been looking in google but can't find it .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.