Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Convertor
- //Kelsey Godwin
- //Converts C to F and F to C (degree of temperatures :) )
- import javax.swing.*;
- public class Convertor {
- public static void main(String[] args) {
- //declare needed variables
- String starterString;
- char tempType = 'N';
- double startTemp = 0;
- double endTemp = 0;
- boolean correctString = false;
- char[] arrayStarterString;
- String split;
- char convertTempType;
- do{
- //grab the input
- starterString = JOptionPane.showInputDialog("Enter your value followed by temp type (C or F) or EXIT to exit program. ");
- //what if they hit cancle? What jerks.
- if(starterString == null)
- {
- JOptionPane.showMessageDialog(null,"Please make sure to enter input!");
- continue;
- }
- //let's nix any spaces before and after
- starterString = starterString.trim();
- //let's make everything uppercase just to trim out half of the error handling we'd need to do!
- starterString = starterString.toUpperCase();
- //if they want to get the hell out of this loop, let's let them
- if (starterString.contentEquals("EXIT"))
- System.exit(0);
- try{
- if ((starterString.charAt(0) >= 58) || (starterString.charAt(0) <= 44) || (starterString.charAt(0) == 47) ){
- JOptionPane.showMessageDialog(null,"Please make sure to start your input with a number.");
- continue;
- }//end of if statement
- }//end of try
- catch(StringIndexOutOfBoundsException e){
- //they just hit enter when putting in input, resulting in nothing. Handle dat error
- JOptionPane.showMessageDialog(null,"Please make sure to enter input!");
- continue;
- }//end of catch
- //if the second to last char is not a space nor a numeral, then the user is a jerk and boot him out
- try {
- if (starterString.charAt(starterString.length() - 2 ) == ' '){
- split = starterString.substring(0,starterString.length() - 2);
- //they were jerks and put a letter somewhere in the number
- try{
- startTemp = Double.valueOf(split);
- }
- catch(NumberFormatException e){
- JOptionPane.showMessageDialog(null,"Please make sure you have a number followed by F or C");
- continue;
- }
- }
- else if ((starterString.charAt(starterString.length() -2) > '9') || (starterString.charAt(starterString.length() -2) < '0')){
- JOptionPane.showMessageDialog(null,"Please make sure to only use C or F for temperature type.");
- continue;
- }
- else{
- split = starterString.substring(0,starterString.length() - 1);
- //they were jerks and put a letter somewhere in the number
- try{
- startTemp = Double.valueOf(split);
- }
- catch(NumberFormatException e){
- JOptionPane.showMessageDialog(null,"Please make sure you have a number followed by F or C");
- continue;
- }
- }
- }//end of try
- catch(StringIndexOutOfBoundsException e){
- //grr, they just entered one number
- JOptionPane.showMessageDialog(null,"Please input a C or F for temperature type at end of input");
- continue;
- }//end of catch
- //finally, convert
- if (starterString.charAt(starterString.length() -1) == 'F'){
- endTemp = convertFtoC(startTemp);
- convertTempType = 'C';
- }
- else if (starterString.charAt(starterString.length() -1) == 'C'){
- endTemp = convertCtoF(startTemp);
- convertTempType = 'F';
- }
- else
- { JOptionPane.showMessageDialog(null,"Please make sure to use C or F for temperature type.");
- continue;
- }
- JOptionPane.showMessageDialog(null, "Converted Temp is " + endTemp + convertTempType);
- }//end of do
- while(correctString == false);
- }//end of main method
- public static double convertFtoC(double startTemp)
- {
- double convertTemp;
- //after you get all the stuff cleaned up, you have a wonderful number to work with
- //so let's convert it FINALLY!
- return convertTemp = (5.0/9.0) * (startTemp - 32);
- }
- public static double convertCtoF(double startTemp)
- {
- double convertTemp;
- //after you get all the stuff cleaned up, you have a wonderful number to work with
- //so let's convert it FINALLY!
- return convertTemp = ((9.0/5.0) * startTemp) + 32;
- }
- }
Add Comment
Please, Sign In to add comment