1

I want to make a program to convert number to letter, from 0-9 to ABCDEFGHIK. For example: with n = 10 the output would be BA as 0 is A is 0 and B is 1. Here is my code:

String convertNumber(long n) {
    String result="";
    String strN = Long.toString(n);
    for (int i=0; i < strN.length();i++){
        char ch = strN.charAt(i);
        switch(ch){
            case '0':
            result = "A";
            case '1':
            result = "B";
            case '2':
            result = "C";
            case '3':
            result = "D";
            case '4':
            result = "E";
            case '5':
            result = "F";
            case '6':
            result = "G";
            case '7':
            result = "H";
            case '8':
            result = "I";
            case '9':
            result = "K";
        }
    }
    return result;
}

However, the results returns only K. Where did I do wrong? Thank you!

ImStuck
  • 13
  • 4

3 Answers3

0

You can use this one:

static String convertNumber(int n) {
        int reminder;
        char[] arr = "ABCDEFGHIK".toCharArray();
        int len = arr.length;
        StringBuilder builder = new StringBuilder();
        while (n != 0) {
            reminder = (int) n % 10;
            n /= 10;
            builder.append(arr[(reminder % len)]);
        }
        return builder.toString();
    }

, main

  static public void main(String[] args) {
        System.out.println(convertNumber(65));
        System.out.println(convertNumber(78));
        System.out.println(convertNumber(99));
        System.out.println(convertNumber(901));
  }

, output

FG
IH
KK
BAK
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
0

There are three mistakes in your program:

  1. Not using break with the case and therefore every case will fall to the last case.
  2. Using = instead of +=
  3. Using the loop in reverse order than the required order. It should be for (int i = strN.length() - 1; i >= 0; i--) instead of for (int i=0; i < strN.length();i++)

Given below is your corrected program:

public class Main {
    public static void main(String[] args) {
        System.out.println(convertNumber(10));
    }

    static String convertNumber(long n) {
        String result = "";
        String strN = Long.toString(n);
        for (int i = strN.length() - 1; i >= 0; i--) {
            char ch = strN.charAt(i);
            switch (ch) {
            case '0':
                result += "A";
                break;
            case '1':
                result += "B";
                break;
            case '2':
                result += "C";
                break;
            case '3':
                result += "D";
                break;
            case '4':
                result += "E";
                break;
            case '5':
                result += "F";
                break;
            case '6':
                result += "G";
                break;
            case '7':
                result += "H";
                break;
            case '8':
                result = "I";
                break;
            case '9':
                result = "K";
            }
        }
        return result;
    }
}

Output:

AB
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

You forgot the break. add break; in every case, like this:

case '0': result = "A"; break; case '1': result = "B"; break;

Marit
  • 2,399
  • 18
  • 27
Shouko
  • 1