-1

I am new to java, and I have just learned to use methods. I wrote a simple program to convert temperatures:

public class TempConversion {

double temperature;

public TempConversion() {

}

public double celsiusToKelvin(double celsiusTemp) {
    temperature = celsiusTemp + 273.15;
    System.out.println("Converted temperature: " + temperature);
    return temperature;
}

public double celsiusToFahrenheit(double celsiusTemp) {
    temperature = celsiusTemp * 9 / 5 + 32;
    System.out.println("Converted temperature: " + temperature);
    return temperature;
}

public double fahrenheitToCelsius(double fahrenheitTemp) {
    temperature = (fahrenheitTemp - 32) * 5 / 9;
    System.out.println("Converted temperature: " + temperature);
    return temperature;
}

public double fahrenheitToKelvin(double fahrenheitTemp) {
    temperature = (fahrenheitTemp + 459.67) * 5 / 9;
    System.out.println("Converted temperature: " + temperature);
    return temperature;
}


public double kelvinToCelsius(double kelvinTemp) {
    temperature = kelvinTemp - 273.15;
    System.out.println("Converted temperature: " + temperature);
    return temperature; 
}

public double kelvinToFahrenheit(double kelvinTemp) {
    temperature = kelvinTemp * 9 / 5 - 459.67;
    System.out.println("Converted temperature: " + temperature);
    return temperature;     
}


public static void main(String[] args) {
    TempConversion temp = new TempConversion();

    temp.celsiusToFahrenheit(38);
    temp.celsiusToKelvin(0);
    
}

}

Right now, however, for the program to convert the temperatures, I have to call each method in the code itself. If I understood right, I can use a Scanner class to get user input, so how would I call one of methods while also using Scanner to get user input. I'm not sure if my question makes sense, but I can try clarifying if asked.

1 Answers1

1

Perhaps It is not the best solution, but I think It is pretty graphic to explain the usefulness of the scanner function in Java.

Just copy and paste this into the main area of your code:


       public static void main(String[] args) {
        TempConversion temp = new TempConversion();
        temp.celsiusToFahrenheit(38);
        temp.celsiusToKelvin(0);

        Double number;
        String input;
        String output;

        Scanner sc = new Scanner(System.in);
        System.out.println("Input a number, only double allowed");

        number = sc.nextDouble();

        sc.nextLine();
        System.out
                .println("Input the first letter of the source unit. c for celsius, f for fahrenheit or k for kelvin");

        input = sc.nextLine();
        System.out
                .println("Input the first letter of the target unit. c for celsius, f for fahrenheit or k for kelvin");

        output = sc.nextLine();

        if (input.equals("c")) {
            if (output.equals("k")) {
                temp.celsiusToKelvin(number);
            } else if (output.equals("f")) {
                temp.celsiusToFahrenheit(number);
            }
        } else if (input.equals("f")) {
            if (output.equals("c")) {
                temp.fahrenheitToCelsius(number);
            } else {
                temp.fahrenheitToKelvin(number);
            }

        } else {
            if (output.equals("c")) {
                temp.kelvinToCelsius(number);
            } else {
                temp.kelvinToFahrenheit(number);
            }
        }
        sc.close();
    }

About how Scanner actually works It is very easy to find it out on the internet, but once you have declared a Scanner object there is no need to declare a new Scanner every time you want to save an input for something else, just as It has been done above, you can just re-use it many times you want.

Once you change from one object to another (in this problem is from keeping the double and now wwe want a String) you have to clear the buffer (there It is that sc.nextLine(); sentence).

And, after all this, remember to close the scanner. It is not mandatory, but if not, you will get a "warning" or something like that.

daenius
  • 38
  • 5
  • thanks so much! just to make sure, the System.out.println... should be in one line, not broken like that? – Anivartin Anand Jul 09 '20 at 00:50
  • @AnivartinAnand Don't worry about the style of the System.out.println, that is how It appears after formating the code. If you work on Eclipse, just use Ctrl + Shift + F, and automatically the code will be formatted. Because the text inside the System's is very long, Eclipse "cut" the System sentence like that. Nothing to worry about! – daenius Jul 09 '20 at 08:55
  • ok, thanks again! I tried running the code, but when I tried compiling, it showed several errors...is that because I put it in the wrong place? I didn't change anything, except taking out the two sample method calls in the main method and replacing it with your code. I also added import java.util.Scanner to the beginning of my code (before specifying the class). Did I do something wrong? – Anivartin Anand Jul 09 '20 at 09:15
  • really? How strange, It shouldn't give you any errors, indeed It works perfectly for me. I've changed my code, now you may see all the code that should be in the ```main (String [] args)``` . The rest of the code you provided is outside the ```main``` section. I don't know If that could be the problem. – daenius Jul 09 '20 at 10:13
  • These are the errors I'm getting: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method celsiusToFahrenheit(int) is undefined for the type TempConversion Scanner cannot be resolved to a type Scanner cannot be resolved to a type The method celsiusToFahrenheit(Double) is undefined for the type TempConversion The method fahrenheitToCelsius(Double) is undefined for the type TempConversion The method fahrenheitToKelvin(Double) is undefined for the type TempConversion The method kelvinToFahrenheit(Double) is undefined for the type TempConversion – Anivartin Anand Jul 09 '20 at 19:24
  • never mind, I fixed it. It was just a simple error on my part with my inconsistency of spelling "fahrenheit" (I had spelled it as farenheit in some places). thanks so much again!! – Anivartin Anand Jul 09 '20 at 19:33
  • Sorry, one more question: what is the purpose for writing "sc.nextLine();" after declaring the "number" variable? – Anivartin Anand Jul 09 '20 at 19:49
  • @AnivartinAnand "Once you change from one object to another (in this problem is from keeping the double and now we want a String) you have to clear the buffer (there It is that sc.nextLine(); sentence)." As you can see, there is no need to rewrite ```sc.nextLine();``` after saving the ```ìnput``` value. – daenius Jul 10 '20 at 00:41
  • ah i see, so if I were to go to an int after the string, I'd have to write `sc.nextInt();`? – Anivartin Anand Jul 10 '20 at 23:02
  • @AnivartinAnand yes, that is. There is ```nextInt()```, ```nextString()```, ```nextDouble()``` and ```nextChar()``` If I remember correctly. Anyway, You can always check it on the Internet if you have doubts, there are plenty of sites about the ```Scanner```function on Java – daenius Jul 13 '20 at 09:54