What is wrong in this code

import java.util.*;
public class smartkeypad
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
String table[] = { " “, “.+@$”, “abc”, “def”, “ghi”, “jkl” , “mno”, “pqrs” , “tuv”, “wxyz” };
String num=obj.next();
char ch=num.charAt(0);
int j = Integer.parseInt(String.valueOf(ch));
String a=table[j];
char ch1=num.charAt(1);
int k = Integer.parseInt(String.valueOf(ch1));
String b=table[k];
func(a,b);
}
public static void func(String a,String b)
{
for(int i=0;i<a.length();i++)
{
for(int j=0;j<b.length();j++)
{
char ch=a.charAt(i);
char ch1=b.charAt(j);
String c=”"+ch+ch1;
System.out.println©;
}
}
}
}

@Zainul17192,

your code only works if input string has a length of 2.

Suggested approach:
Your are given this string input table:
string table[] = { " ", “.+@$”, “abc”, “def”, “ghi”, “jkl” , “mno”, “pqrs” , “tuv”, “wxyz” };

Here if you press 0 it will give you " ", if you press 1 it will give “.+@$” and so on.

In the output you have to print all possible string combinations for a input string string say 12.
If you press 1, possible characters are: .+@$
If you press 2, possible characters are: abc
Hence output will be combination of all the characters.

Use recursion.

  • Bigger Problem : To Print the codes for given whole length String.
  • Smaller Problem : Assume the recursion works and will give you ans for String of length n - 1 .
    Self Work : In order to make you smaller prblm your problem all you need to work for the first character and add one by one your all possible codes in the ans and the work will be done.

@Zainul17192,
In the question it is mentioned that length of string is between 1 and 10.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.