Ryan D. Ryan D. - 1 month ago 6
Java Question

My Java methods are not running correctly and causing my program to compile incorrectly

I am writing a program that will prompt the user to enter a temp as a an integer, then prompt the user to select what scale that temp is in, either fahrenheit, celsius, or kelvin. Then it prompts the user to select what temp they would like to convert to, from the 3 scale choices, then calculates the conversion and displays the final temp. My program currently runs but incorrectly and I have been staring at this for a couple of days now and I am not sure why it is compiling the way it is. I am still fairly new to coding this is a class assignment that I have been working on just looking for some advice.

package tempconverter;
import java.util.*;

public class TempConverter
{

public static void main(String[] args)
{
double temp1;
double convertTo;
double finalTemp;
double currScale;
char cont='E';
char process='P';
int scale1=0;
int scale2=0;

do
{
//input
getTemp();
scaleOne(scale1);
toScale(scale2);

//process


System.out.print("Your final temp is: " +convert()+toScale(scale2));

}while(cont=='N');
}
private static double getTemp()
{
double temp;
double currentTemp;
Scanner userIn= new Scanner(System.in);
System.out.print("Enter a temperature to convert ");
while(!userIn.hasNextDouble())
{
System.out.println("That is not a valid temperature!");
System.out.print("Re-enter a temperature: ");
userIn.next();
}
temp=userIn.nextDouble();



return temp;
}

private static String scaleOne(int userChoice)
{

Scanner userIn= new Scanner(System.in);



System.out.print("What scale is your temp currently in?"+
"1: Fahrenheit" +
"2: Celsius" +
"3: Kelvin" +
"Please enter 1, 2, or 3: ");


String scale="";

switch(userChoice)
{
case 1:
{scale= "Fahrenheit";
break;}

case 2:
{scale="Celsius";
break;}

case 3:
{scale="Kelvin";
break;}

default:
{scale= "There was an error in your choice!";}
}

return scale;

}



private static String toScale(int userChoice)
{

Scanner userIn= new Scanner(System.in);



System.out.print("What scale would you like to convert to?"+
"1: Fahrenheit" +
"2: Celsius" +
"3: Kelvin" +
"Please enter 1, 2, or 3: ");


String scale2="";

switch(userChoice)
{
case 1:
{scale2= "Fahrenheit";
break;}

case 2:
{scale2="Celsius";
break;}

case 3:
{scale2="Kelvin";
break;}

default:
{scale2= "There was an error in your choice!";}
}

return scale2;

}



public static double convert()
{
double farToKel;
double farToCel;
double celToKel;
double celToFar;
double kelToFar;
double kelToCel;
int scale1=0;
double converts;

if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Kelvin")
{
converts=farToKel=(getTemp()+459.67)* 5/9;
}

else if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Celsius")
{
converts=farToCel=(getTemp()-32)*5/9;
}

else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Kelvin")
{
converts=celToKel=getTemp()+273.15;
}

else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Fahrenheit")
{
converts=celToFar=getTemp()*9/5+32;
}

else if(scaleOne(scale1)=="Kelvin" && toScale(scale1)=="Fahrenheit")
{
converts=kelToFar=getTemp()*9/5-459.67;
}

else
{
converts=kelToCel=getTemp()-276.15;
}

return converts;
}








}


And here is what it compiles

run:
Enter a temperature to convert 100
What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale would you like to convert to?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: What scale is your temp currently in?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: Enter a temperature to convert 2
What scale would you like to convert to?1: Fahrenheit2: Celsius3: KelvinPlease enter 1, 2, or 3: Your final temp is: -274.15There was an error in your choice!BUILD SUCCESSFUL (total time: 33 seconds)

Its like it is not compiling in the right order, or getting the correct information in the right order and get some information over and over again. Once you enter in the numeric temp, it prompts you for the scale but not just once it prints that out a bunch of times, then you enter your choice for the first scale and it just goes all the way to completion without you entering in your choice for toScale.

Answer

See the comments inserted and corrections made in your code below.

public class TempConverter {

    // variables that are needed in more than one method must be declared outside the methods
    // (static since the methods are static; not recommended, but let it be for now)
    static double temp1;
    static String scale1;
    static String scale2;

    public static void main(String[] args) {
        // get rid of unused variables
        char cont = 'E';

        do {
            // input
            // save all inputs to variables
            temp1 = getTemp();
            // Java parameters are pass by value, so to get a value out of the method, use the returned value
            // then you don’t need the parameter
            scale1 = scaleOne();
            scale2 = toScale();

            // process
            // don’t call toScale() again, or the user will be prompted again,
            // instead, use the value already stored in scale2
            System.out.print("Your final temp is: " + convert() + ' ' + scale2);

            // you should set cont to something if you want the loop to repeat

        } while (cont == 'N');
    }

    private static double getTemp() {
        double temp;
        Scanner userIn = new Scanner(System.in);
        System.out.print("Enter a temperature to convert ");
        while (!userIn.hasNextDouble()) {
            System.out.println("That is not a valid temperature!");
            System.out.print("Re-enter a temperature: ");
            userIn.next();
        }
        temp = userIn.nextDouble();

        return temp;
    }

    private static String scaleOne() {

        Scanner userIn = new Scanner(System.in);

        // \n is a line break for readability
        System.out.print("What scale is your temp currently in?" 
                + "\n1: Fahrenheit" 
                + "\n2: Celsius" 
                + "\n3: Kelvin"
                + "\nPlease enter 1, 2, or 3: ");

        // read a value from the user
        int userChoice = userIn.nextInt();

        String scale = "";

        switch (userChoice) {
        case 1: {
            scale = "Fahrenheit";
            break;
        }

        case 2: {
            scale = "Celsius";
            break;
        }

        case 3: {
            scale = "Kelvin";
            break;
        }

        default: {
            scale = "There was an error in your choice!";
        }
        }

        return scale;

    }

    private static String toScale() {

        Scanner userIn = new Scanner(System.in);

        System.out.print("What scale would you like to convert to?" + "1: Fahrenheit" + "2: Celsius" + "3: Kelvin"
                + "Please enter 1, 2, or 3: ");

        String scale2 = "";

        int userChoice = userIn.nextInt();

        // ideally should not accept the same scale as scale1 (from scale)
        switch (userChoice) {
        case 1: {
            scale2 = "Fahrenheit";
            break;
        }

        case 2: {
            scale2 = "Celsius";
            break;
        }

        case 3: {
            scale2 = "Kelvin";
            break;
        }

        default: {
            scale2 = "There was an error in your choice!";
        }
        }

        return scale2;

    }

    public static double convert() {
        // need only one variable for result
        double converts;

        // don’t call methods again, use variables
        // don’t use == for comparing strings, use .equals
        // typo: the second scale corrected to scale2
        if (scale1.equals("Fahrenheit") && scale2.equals("Kelvin")) {
            // don’t call getTemp() again
            converts = (temp1 + 459.67) * 5 / 9;
        }

        else if (scale1.equals("Fahrenheit") && scale2.equals("Celsius")) {
            converts = (temp1 - 32) * 5 / 9;
        }

        else if (scale1.equals("Celsius") && scale2.equals("Kelvin")) {
            converts = temp1 + 273.15;
        }

        else if (scale1.equals("Celsius") && scale2.equals("Fahrenheit")) {
            converts = temp1 * 9 / 5 + 32;
        }

        else if (scale1.equals("Kelvin") && scale2.equals("Fahrenheit")) {
            converts = temp1 * 9 / 5 - 459.67;
        }

        else { // Kelvin to Celsius
            converts = temp1 - 276.15;
        }

        return converts;
    }

}

I believe it basically works now.