Advertisement
Guest User

Untitled

a guest
Feb 9th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class LettersChangeNumbers {
  5. public static void main(String[] args){
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. String[] inputSplitted = sc.nextLine().split("\\s+");
  9.  
  10. double firstTempResult = 1;
  11. double secondTempResult = 0;
  12. double finalResult;// = 0;
  13.  
  14. ArrayList<String> inputSplittedAsArray = new ArrayList<String>();
  15.  
  16. for (int i = 0; i < inputSplitted.length; i++) {
  17. inputSplittedAsArray.add(inputSplitted[i]);
  18. }
  19.  
  20. firstTempResult = calculateTempInt(inputSplittedAsArray.get(0));
  21.  
  22. for (int i = 1; i < inputSplittedAsArray.size(); i++) {
  23. while (i < inputSplittedAsArray.size() - 1) {
  24. firstTempResult = sumTempResults(firstTempResult, calculateTempInt(inputSplittedAsArray.get(i)));
  25. i = i+1;
  26. }
  27.  
  28. secondTempResult = calculateTempInt(inputSplitted[inputSplittedAsArray.size() - 1]);
  29. }
  30.  
  31. if (inputSplitted.length < 2) {
  32. finalResult = firstTempResult;
  33. }
  34. else {
  35. finalResult = sumTempResults(firstTempResult, secondTempResult);
  36. }
  37.  
  38. System.out.printf("%.2f", finalResult);
  39.  
  40. }
  41.  
  42. public static double sumTempResults(double firstTempResult, double secondTempResult){
  43. double sum = firstTempResult + secondTempResult;
  44. return sum;
  45. }
  46. public static double calculateTempInt(String theString){
  47. int tempInt = extractIntFromString(theString);
  48. double tempResult = 1;
  49. if (checkUpperCase(theString.charAt(0))) {
  50.  
  51. tempResult = (double)tempInt / getPositionInAlphabetOfChar(theString.charAt(0));
  52. }
  53. else if (checkLowerCase(theString.charAt(0))) {
  54. tempResult = (double)tempInt * getPositionInAlphabetOfChar(theString.charAt(0));
  55. }
  56.  
  57. if (checkUpperCase(theString.charAt(theString.length() - 1))) {
  58. tempResult = (double)tempResult - getPositionInAlphabetOfChar(theString.charAt(theString.length() - 1));
  59. }
  60. else if (checkLowerCase(theString.charAt(theString.length() - 1))) {
  61. tempResult = (double)tempResult + getPositionInAlphabetOfChar(theString.charAt(theString.length() - 1));
  62. }
  63.  
  64. return tempResult;
  65. }
  66.  
  67. private static int extractIntFromString(String theString){
  68.  
  69. String theIntAsString = theString.substring(1, theString.length() - 1);
  70. int theInt = Integer.parseInt(theIntAsString);
  71. return theInt;
  72. }
  73.  
  74. public static int getPositionInAlphabetOfChar(char theChar){
  75.  
  76. int asciiResult = (int)theChar;
  77.  
  78. if (checkUpperCase(theChar)) {
  79. asciiResult = asciiResult - 64;
  80. }
  81. if (checkLowerCase(theChar)) {
  82. asciiResult = asciiResult - 96;
  83. }
  84. return asciiResult;
  85. }
  86.  
  87. public static boolean checkUpperCase(char theChar){
  88. boolean result = Character.isUpperCase(theChar);
  89. return result;
  90.  
  91. }
  92.  
  93. public static boolean checkLowerCase(char theChar){
  94. boolean result = Character.isLowerCase(theChar);
  95. return result;
  96.  
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement