rhodium-chloride
rhodium-chloride

Reputation: 3

How can I properly initialize a variable created in a separate method, to be used in another method?

I have made two methods that take user input and should return two separate variables, that I want to call in a third method. However, I don't understand fully how to "link" the variables into the third method. It's also necessary that I keep all three methods separate, the calculate method, askLen and askWid.

I've tried a few things like declaring the integers in different places, using final int, or long, double, etc.

 public static void main (String[] p)
  {
     askLen();
     askWid();
     calculate();
     System.exit(0);
  }

  public static int askLen ()
  {
     final int len;    
     Scanner scanner = new Scanner(System.in);
     System.out.println("What is the length of the room (in cm)?");
     len = Integer.parseInt(scanner.nextLine());
     final long l=len;
     return len;
  }

  public static int askWid ()
  {
     final int wid;    
     Scanner scanner = new Scanner(System.in);
     System.out.println("What is the width of the room (in cm)?");
     wid = Integer.parseInt(scanner.nextLine());
     final long w=wid;
     return wid;
  }

  public static int calculate ()
  {
     final int len;
     final long l;
     final int wid;
     final long w;   
     long area = (l * w) / 10000;
     System.out.println("The area is " + area + "m^2");
     double wastage = (area * 1.10) / 100;
     System.out.println("The extra you need for wastage is " + wastage + "m^2");
     double areaTotal = area + wastage;
     System.out.println("The total flooring area to order is: " + areaTotal + "m^2");
     return 1;
  }

My expected result is that it correctly takes the user's integer inputs and does the arithmetic that I have coded, returning a final result of the strings above with the correct calculated answers.

The error messages I'm getting during compiling are:

flooring.java:46: error: variable l might not have been initialized
         long area = (l * w) / 10000;
                      ^
flooring.java:46: error: variable w might not have been initialized
         long area = (l * w) / 10000;
                          ^
2 errors

Upvotes: 0

Views: 245

Answers (1)

Leftii
Leftii

Reputation: 61

Why not just pass the variables to the methods? Something like:

 public static void main (String[] p)
  {
     int length = askLen();
     int width = askWid();
     calculate(length, width);
     System.exit(0);
  }

  public static int askLen ()
  {
     final int len;    
     Scanner scanner = new Scanner(System.in);
     System.out.println("What is the length of the room (in cm)?");
     len = Integer.parseInt(scanner.nextLine());
     final long l=len;
     return len;
  }

  public static int askWid ()
  {
     final int wid;    
     Scanner scanner = new Scanner(System.in);
     System.out.println("What is the width of the room (in cm)?");
     wid = Integer.parseInt(scanner.nextLine());
     final long w=wid;
     return wid;
  }

  public static void calculate (int len, int wid)
  {
     long area = (len * wid) / 10000;
     System.out.println("The area is " + area + "m^2");
     double wastage = (area * 1.10) / 100;
     System.out.println("The extra you need for wastage is " + wastage + "m^2");
     double areaTotal = area + wastage;
     System.out.println("The total flooring area to order is: " + areaTotal + "m^2");
  }

Also, in calculate() the variables l and w are never set to anything which is giving you the error. By passing in the variables you don't need to set/use l or w and can instead use the variables passed in the parenthesis wid and len. You also don't need a return type or return for calculate() unless you need to store the value in a variable from where you call the method.

Upvotes: 2

Related Questions