Friday 14 July 2017

Java Code to convert a decimal representation for a number to the corresponding character string representation.

Problem:

Design an algorithm to convert a decimal representation for a number to the corresponding character string representation.

Solution:

package com.myprograms;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DecimalToCharatcerConversion {

Integer number;
List<Integer> digits = new ArrayList<Integer>();
List<Character> result = new ArrayList<Character>();

public static void main(String[] args) {
DecimalToCharatcerConversion characterToIntegerConversion = new DecimalToCharatcerConversion();
characterToIntegerConversion.getTheNUmberAsString();
characterToIntegerConversion.convertToCharacter();
characterToIntegerConversion.printTheResult();

}

public void getTheNUmberAsString(){
Scanner s = new Scanner(System.in);
System.out.println("enter the number" );
number = s.nextInt();
s.close();
}

public void convertToCharacter(){
for(Character c: number.toString().toCharArray()){
digits.add((int)c);
}
}

public void printTheResult(){
for(int n : digits){
result.add((char)(n));
}
System.out.println("The ascii values are: " + digits);
System.out.println("the character values are: "  + result);
}
}


Output:

enter the number
198765
The ascii values are: [49, 57, 56, 55, 54, 53]
the character values are: [1, 9, 8, 7, 6, 5]


No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...