Guest User

Untitled

a guest
Jul 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. //Convertor
  2. //Kelsey Godwin
  3. //Converts C to F and F to C (degree of temperatures :) )
  4.  
  5. import javax.swing.*;
  6.  
  7. public class Convertor {
  8.  
  9. public static void main(String[] args) {
  10. //declare needed variables
  11. String starterString;
  12. char tempType = 'N';
  13. double startTemp = 0;
  14. double endTemp = 0;
  15. boolean correctString = false;
  16. char[] arrayStarterString;
  17. String split;
  18. char convertTempType;
  19.  
  20.  
  21. do{
  22. //grab the input
  23. starterString = JOptionPane.showInputDialog("Enter your value followed by temp type (C or F) or EXIT to exit program. ");
  24.  
  25. //what if they hit cancle? What jerks.
  26. if(starterString == null)
  27. {
  28. JOptionPane.showMessageDialog(null,"Please make sure to enter input!");
  29. continue;
  30. }
  31.  
  32. //let's nix any spaces before and after
  33. starterString = starterString.trim();
  34.  
  35. //let's make everything uppercase just to trim out half of the error handling we'd need to do!
  36. starterString = starterString.toUpperCase();
  37.  
  38. //if they want to get the hell out of this loop, let's let them
  39. if (starterString.contentEquals("EXIT"))
  40. System.exit(0);
  41.  
  42. try{
  43. if ((starterString.charAt(0) >= 58) || (starterString.charAt(0) <= 44) || (starterString.charAt(0) == 47) ){
  44. JOptionPane.showMessageDialog(null,"Please make sure to start your input with a number.");
  45. continue;
  46. }//end of if statement
  47.  
  48. }//end of try
  49. catch(StringIndexOutOfBoundsException e){
  50. //they just hit enter when putting in input, resulting in nothing. Handle dat error
  51. JOptionPane.showMessageDialog(null,"Please make sure to enter input!");
  52. continue;
  53.  
  54. }//end of catch
  55.  
  56. //if the second to last char is not a space nor a numeral, then the user is a jerk and boot him out
  57. try {
  58. if (starterString.charAt(starterString.length() - 2 ) == ' '){
  59. split = starterString.substring(0,starterString.length() - 2);
  60.  
  61. //they were jerks and put a letter somewhere in the number
  62. try{
  63. startTemp = Double.valueOf(split);
  64. }
  65. catch(NumberFormatException e){
  66. JOptionPane.showMessageDialog(null,"Please make sure you have a number followed by F or C");
  67. continue;
  68. }
  69. }
  70. else if ((starterString.charAt(starterString.length() -2) > '9') || (starterString.charAt(starterString.length() -2) < '0')){
  71. JOptionPane.showMessageDialog(null,"Please make sure to only use C or F for temperature type.");
  72. continue;
  73. }
  74. else{
  75. split = starterString.substring(0,starterString.length() - 1);
  76.  
  77. //they were jerks and put a letter somewhere in the number
  78. try{
  79. startTemp = Double.valueOf(split);
  80. }
  81. catch(NumberFormatException e){
  82. JOptionPane.showMessageDialog(null,"Please make sure you have a number followed by F or C");
  83. continue;
  84. }
  85. }
  86. }//end of try
  87. catch(StringIndexOutOfBoundsException e){
  88. //grr, they just entered one number
  89. JOptionPane.showMessageDialog(null,"Please input a C or F for temperature type at end of input");
  90. continue;
  91. }//end of catch
  92.  
  93.  
  94. //finally, convert
  95. if (starterString.charAt(starterString.length() -1) == 'F'){
  96. endTemp = convertFtoC(startTemp);
  97. convertTempType = 'C';
  98. }
  99. else if (starterString.charAt(starterString.length() -1) == 'C'){
  100. endTemp = convertCtoF(startTemp);
  101. convertTempType = 'F';
  102. }
  103. else
  104. { JOptionPane.showMessageDialog(null,"Please make sure to use C or F for temperature type.");
  105. continue;
  106. }
  107.  
  108. JOptionPane.showMessageDialog(null, "Converted Temp is " + endTemp + convertTempType);
  109. }//end of do
  110. while(correctString == false);
  111.  
  112. }//end of main method
  113.  
  114. public static double convertFtoC(double startTemp)
  115. {
  116. double convertTemp;
  117.  
  118. //after you get all the stuff cleaned up, you have a wonderful number to work with
  119. //so let's convert it FINALLY!
  120. return convertTemp = (5.0/9.0) * (startTemp - 32);
  121.  
  122. }
  123. public static double convertCtoF(double startTemp)
  124. {
  125. double convertTemp;
  126.  
  127. //after you get all the stuff cleaned up, you have a wonderful number to work with
  128. //so let's convert it FINALLY!
  129. return convertTemp = ((9.0/5.0) * startTemp) + 32;
  130.  
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment