Advanced Java questions

A program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ASCII values of the characters of each string. The program should then subtract the sum of the ASCII values of the second string from the sum of the ASCII values of the first string.

Suppose the following input is given to the program:

123ABC,456DEF

Then the sum of the ASCII values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18
The corresponding output to be printed by the program is:

-18

Solution:
import java.util.Scanner;



public class Solution {

 /**
  * @param args
  */
 public static void main(String[] args) {
  boolean condition = false;
  do {
   Scanner scanner = new Scanner(System.in);
   String value = scanner.nextLine();
   condition = value.equalsIgnoreCase("exit");
   if(!condition && value.contains(",")){
    calculate(value);
   }
  } while (!condition);

 }
 
 private static void calculate(String value){
   
   
   
   final String[] event1 = value.split(",");
   int ss = 0;
   for ( int i = 0; i < event1[0].length(); ++i ) {
          char c = event1[0].charAt( i );
          ss += (int) c;
          //System.out.println(skype);
         }
   int sd = 0;
   for ( int i = 0; i < event1[1].length(); ++i ) {
          char c = event1[1].charAt( i );
          sd += (int) c;
          //System.out.println(sd);
         }
   System.out.println(ss-sd);
  
 }

}



Comments

Popular posts from this blog

XSLT - Modify Date and DateTime value

Integrate With Mutual Certificate Authentication based Service

Yield Price Sri Lanka - Android Application