Alex Wright
Alex Wright

Reputation: 9

Passing values from one nonstatic method to another from main?

I'm having trouble using non static methods. I'm trying to get the value of my "side" variable from a method and plug them into another method which will calculate area. Is there a way to do this without changing the methods to static? None of the previously answered questions on here are helping and neither is my textbook.

import java.util.*;
public class CubeVolume
{
    int side1;
    int side2;
    int side3;

public void getSides()
{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the length of side1");
    side1 = input.nextInt();
    System.out.println("Enter the length of side2");
    side2 = input.nextInt();
    System.out.println("Enter the length of side3");
    side3 = input.nextInt();
}

public int getVolume(int side1, int side2, int side3)
{
    int volume = side1 * side2 * side3;
    return volume;
}
public static void main(String[] args)
{
    CubeVolume cube = new CubeVolume();
    cube.getSides();
    cube.getVolume(side1, side2, side3);
 }
}

I think the problem is with my method call cube.getVolume(side1, side2, side3); because the compiler tells me that non-static variable cannot be referenced from a static context.

Upvotes: 0

Views: 136

Answers (3)

Sash Sinha
Sash Sinha

Reputation: 22410

There is no need to pass in any parameters to getVolume(), just use the class variables:

import java.util.Scanner;

class CubeVolume {
    private int side1;
    private int side2;
    private int side3;

    private void getSides() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the length of side1: ");
        side1 = input.nextInt();
        System.out.print("Enter the length of side2: ");
        side2 = input.nextInt();
        System.out.print("Enter the length of side3: ");
        side3 = input.nextInt();
        input.close();
    }

    private int getVolume() {
        return side1 * side2 * side3;
    }

    private void printAppTitle() {
        System.out.println("Cube Volume Calculator");
        System.out.println("======================");
    }

    public static void main(String[] args) {
        CubeVolume cube = new CubeVolume();
        cube.printAppTitle();
        cube.getSides();
        String cubeVolumeString = String.valueOf(cube.getVolume());
        System.out.println("The cubes volume is: " + cubeVolumeString);
    }
}

Example Usage:

Cube Volume Calculator
======================
Enter the length of side1: 3
Enter the length of side2: 4
Enter the length of side3: 5
The cube's volume is: 60

Alternative approach which stores the side lengths in a double array, sides, and deals with possible invalid input in getSides():

import java.util.Scanner;

class CubeVolume {
  private double[] sides;

  CubeVolume() {
    sides = new double[3];
  }

  private void getSides() {
    Scanner scanner = new Scanner(System.in);
    int currentSide = 0;
    while (currentSide < sides.length) {
      System.out.printf("Enter the length of side %d: ", currentSide + 1);
      double nextSide = 0.0;
      input:
      while (scanner.hasNext()) {
        if (scanner.hasNextDouble()){
          nextSide = scanner.nextDouble();
          if (nextSide > 0) {
            sides[currentSide] = nextSide;
            break input;
          } else {
            System.out.println("ERROR: Input number was too small.");
            System.out.printf("Enter the length of side %d: ", currentSide + 1);
          }
        } else {
          System.out.println("ERROR: Invalid input, please input a number.");
          System.out.printf("Enter the length of side %d: ", currentSide + 1);
          scanner.next();
        }
      }
      currentSide++;
    }
    scanner.close();
  }

  private double getVolume() {
    return sides[0] * sides[1] * sides[2];
  }

  private void printAppTitle() {
    System.out.println("Cube Volume Calculator");
    System.out.println("======================");
  }

  public static void main(String[] args) {
    CubeVolume cube = new CubeVolume();
    cube.printAppTitle();
    cube.getSides();
    String cubeVolumeString = String.format("%.2f", cube.getVolume());
    System.out.println("The cube's volume is: " + cubeVolumeString);
  }
}

Example Usage 2:

Cube Volume Calculator
======================
Enter the length of side 1: a
ERROR: Invalid input, please input a number.
Enter the length of side 1: -1.1
ERROR: Input number was too small.
Enter the length of side 1: 3.4
Enter the length of side 2: 4.7
Enter the length of side 3: 5.8
The cube's volume is: 92.68

Upvotes: 1

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

If you want to use methods within main() then those methods must be static since main() is static. So you methods should be declared as:

public static void getSides() { .... }
public static int getVolume(int side1, int side2, int side3) { .... }

You can however avoid all this if you tell an instance of your class to start from a non-static method from within your main() method:

public static void main(String[] args) {
    new CubeVolume().startApp(args);
}

private void startApp(String[] args) {
    CubeVolume cube = new CubeVolume();
    cube.getSides();
    cube.getVolume(side1, side2, side3);
}

Now your other methods in the class do not need to be static since you're not calling them from static main().

Upvotes: 1

Ved_Code_it
Ved_Code_it

Reputation: 774

You need not to pass sides parameters to get valume function because your sides variable will be available to get valume function.

Upvotes: 0

Related Questions