Hi,
I have written a simple converter from Celsius to Fahrenheit and vise versa. I want write to file what conversion was chosen by user, what value was entered and result.
There is my code:
User input class
package Converter.learning; //This class handles user input import java.util.Scanner; public class UserInput { public UserInput(){ //dataInput(); } //declaring variables float temp; //Creating scanner for reading user input Scanner inputTemp= new Scanner(System.in); //Method that does handles input public float dataInput(){ System.out.println("Enter a numer"); //Checking if an input is a float while (!inputTemp.hasNextFloat()){ System.out.println("You have entered wrong type. Please enter number!"); inputTemp.next(); } temp = inputTemp.nextFloat(); //Closing Scanner inputTemp.close(); return temp ; } }
Conversion Class
package Converter.learning; //This class converts C to F and vice versa public class Conversion { //declaring variables float result; float C; float F; //Constructor public Conversion(){ //convertToC(F); //convertToF(C); //print(); } //Method that does conversion from C to F public float convertToF (float C){ return result = C * 9/5 + 32; } //Method that does conversion from F to C public float convertToC (float F){ return result = (F - 32) * 5/9; } public void print(){ System.out.println("Result is: " + result); } }
Main Class
package Converter.learning; import java.util.Scanner; /* * Program is a simple converter that converts temperature. Celsius to * Fahrenheit and oposite. * Version 3.4 */ public class Main { public static void main(String[] args) { String s ; UserInput input = new UserInput(); Conversion conversion = new Conversion(); System.out.println("To convert Celsius to Fahrenheit press C, To convert Fahrenheit to Celsius press F "); //Creating a scanner that enables conversion mode Scanner scan = new Scanner(System.in); while(!scan.hasNext("[CF]")){ System.out.println("wrong input, please enter C or F"); scan.next(); //mySet.add(); } //mySet.add(s = scan.next()); s = scan.next(); switch(s){ case "C": System.out.println("Conversion To Fahrenheit"); conversion.convertToF(input.dataInput()); conversion.print(); scan.close(); break; case "F": System.out.println("Conversion To Celsius"); conversion.convertToC(input.dataInput()); conversion.print(); scan.close(); break; default: break; } //input.dataInput(); //System.out.println(input.temp); } }
Do you have any ideas?