user13267091
user13267091

Reputation: 59

Display characters for the number the user gives as input

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

Upvotes: 1

Views: 6511

Answers (3)

Theresa Mary
Theresa Mary

Reputation: 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);
     }
 }

Upvotes: -1

thisisjaymehta
thisisjaymehta

Reputation: 643

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
    }
}

Upvotes: 0

Sidak
Sidak

Reputation: 1283

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'

Upvotes: 2

Related Questions