1

Rohan wants a magic board, which displays a character for a corresponding number for his science exhibition. Help him to develop such application.

For example when the digits 65666768 are entered, the alphabet ABCD are to be displayed

[Assume the number of inputs should be always 4]

Sample Input 1: Enter the digits 65, 66, 67, 68, Sample Output 1: 65-A, 66-B, 67-C , 68-D,

Sample Input 2: Enter the digits: 115, 116 , 101 , 112, Sample Output 2: 115-s, 116-t, 101-e, 112-p,

I don't know how to execute this program while getting input from the user

user13267091
  • 49
  • 4
  • 9
  • 2
    I suggest you read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Here are some excerpts: (1) It is okay to ask about homework. (2) Make a good faith attempt to solve the problem yourself first. (3) Ask about specific problems with your existing implementation. – Abra Apr 11 '20 at 09:42

3 Answers3

2

Have a look at Scanner to read input from the user and storing it into variables, then just convert the int value you got as input to characters.

Its quite simple to convert an int into its corresponding character in Java, just cast it.

eg.

int i = 65;
char c = (char) i; // this will set value of c to 'A'
Sidak
  • 975
  • 2
  • 9
  • Can you give a eg. that involves the use of scanner with a little more details – user13267091 Apr 11 '20 at 09:52
  • Have a look at the top answer [here](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – Sidak Apr 11 '20 at 09:53
0

Try something like

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        int no;            //variable to store input number
        String ans = "";   //string to store ans
        Scanner sc = new Scanner(System.in);
        no = sc.nextInt(); //Take input
        while(no > 0)
        {
            ans = (char)(no%100) + ans;  //Convert last two digits to char and append ahead of answer string
            no /= 100;     //Remove those last two digits
        }
        System.out.println(ans);  //Print ans
    }
}
thisisjaymehta
  • 600
  • 1
  • 8
  • 21
-1
 import java.util.Scanner;
 public class AsciValue{
     public static void main(String args[]){
         
         Scanner sc=new Scanner(System.in);
         
         System.out.println("Enter the digits:");
         int a=sc.nextInt();
         int b=sc.nextInt();
         int c=sc.nextInt();
         int d=sc.nextInt();
        
         char i=(char)a;
         char j=(char)b;
         char k=(char)c;
         char l=(char)d;
        
         System.out.println(a+"-"+i);
         System.out.println(b+"-"+j);
         System.out.println(c+"-"+k);
         System.out.println(d+"-"+l);
     }
 }