pasar variable al método principal java

I am trying to write a simple program that has two classes. I want one class (with the main method) to handle all the input and output and the other class to handle all of the mathematics then return the calculations to the main method. I can successfully pass variables from main method to an object in the mathematics class and have tested the results in that method with a println but can't seem to pass the finished calculations back to my main method. Here is my code, please help me understand. Thank you very much Here is class with main method

import java.util.Scanner;


public class io {
    public static void main (String[] args){
        Scanner chargeTankStartGaugeFeetInput = new Scanner(System.in);
        Scanner chargeTankStartGaugeInchesInput = new Scanner(System.in);
        System.out.println("What is the charge tank's start gauge feet: ");

        String chargeTankStartGaugeFeet = chargeTankStartGaugeFeetInput.nextLine();
        System.out.println("What is the charge tank's start gauge inches: ");

        String chargeTankStartGaugeInches = chargeTankStartGaugeInchesInput.nextLine();

        math mathObject = new math();
        mathObject.changeGaugesToInches(chargeTankStartGaugeFeet, 
                                        chargeTankStartGaugeInches);
        System.out.println(mathObject.totalInches(totalInches) 
                           + " is total inches in io");     
    }
}

I get an error that says "totalInches" in the main method cannot be resolved to a variable. Is my thinking even close as to how this is supposed to work?

And here is the math class

public class math {
    public void changeGaugesToInches(String arg1, String arg2) {
        double arg1Double = Double.valueOf(arg1).doubleValue();
        double arg2Double = Double.valueOf(arg2).doubleValue();

        double totalInches = arg1Double * 12 + arg2Double;

        System.out.println(totalInches + " is the total inches");
    }
}

preguntado el 26 de agosto de 12 a las 23:08

3 Respuestas

You can return the value from the method...

public double changeGaugesToInches(...)
{
  ....
  return totalIncehs;
}

Respondido 26 ago 12, 23:08

Firstly, by convention, all class and enum names in java should begin with a capital letter. Secondly, you may want to use a more descriptive name for your math class, "UnitsConverter" if that is all that it does.

In changeGaugesToInches, you should rename arg1 y arg2 a feet y inches.

Most importantly, you need to change the method to return the result, and assign it to a variable in your main method:

double totalInches = mathObject.changeGaugesToInches(chargeTankStartGaugeFeet, chargeTankStartGaugeInches);

public double changeGaugesToInches(String arg1, String arg2){
    //...
    return totalInches;  
}

Because this method does not use any instance variables, unless you think you might over ride this method in a subclass (to add metric units, for example) the code would be more efficient if you declared it as static. Also, you can probably use integers unless you require more accuracy.

double totalInches = UnitsConverter.changeGaugesToInches(chargeTankStartGaugeFeet, chargeTankStartGaugeInches);

public static int changeGaugesToInches(String feet, String inches){
    return changeGaugesToInches( Integer.parseInt(feet), Integer.parseInt(inches) );
}

// this method can be used more efficiently from parts of your app that already have the units as integers.
public static int changeGaugesToInches(int feet, int in
    //...
    return totalInches;  
}

Respondido 27 ago 12, 00:08

Muchas gracias por la ayuda - TeachMePlease

Any void method can't have return value. Since, public void changeGaugesToInches(String arg1, String arg2) is a void method therefore it has no return type.

If you make it static then you can't use math mathObject = new math();

Respondido 08 Abr '15, 08:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.