xAndy
xAndy

Reputation: 3

Division between float and int won't compile

First off, I'm still pretty new to Java so excuse me if the solution is something obvious.

I wrote code which is supposed to divide every second number in a specific range of numbers (from -5 to 20 in this example) with a float number but Java won't compile.

  public class exerciseD {
     static float Division (float r) {

      int n = (-5);
      float x = 0;

      if (n <= 20) {
       float x = n * r; 
       System.out.println(x);
       n = n + 2; 
      }
      else 
          return x;
  }
  public static void main(String[ ] args) {

      float y = Divison(22.5);
      System.out.println(y);

      }
  }

Error Message :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Divison(double) is undefined for the type exerciseD
at exerciseD.main(exerciseD.java:45)

What did I do wrong in this code? I just can't tell what the issue with double is. Every variable is either a float or int isn't it?

Thank you.

Upvotes: 0

Views: 239

Answers (1)

Basil Battikhi
Basil Battikhi

Reputation: 2668

Well, You have Multiple errors the first one at if statement you are defining another X variable which is already defined above. the second error is the method name is Division and not Divison

The Third Error you cannot pass double to a float variable as parameter since you called Division(22.5) it should be Division(22.5F) to indicate that it's float otherwise it will pass it as double which will be incompatible types possible lossy conversion from double to float

the fourth error is you are missing the return statement of the Division method at if block

Upvotes: 2

Related Questions