Home > OS >  Trying to print out different message depending on the value of an instance
Trying to print out different message depending on the value of an instance

Time:09-21

heres my code:

public class auditorForum {
   int pastMonthSpend;

  public auditorForum(int spending){
      spending = pastMonthSpend;
  }
  public static void main(String[] args) {
      auditorForum October = new auditorForum(1433);
      if (October >= 1000) {
        if (October <= 1500){
          System.out.println("Eligible");
        } else {
          System.out.println("Ineligible");
        }
      }
    }
}

It results in

auditorForum.java:9: error: bad operand types for binary operator '>='
      if (October >= 1000) {
                  ^
  first type:  auditorForum
  second type: int
1 error

and im not sure what the fix is, ive tried making separate values for the minimum and maximum values (1000 and 1500) but no

CodePudding user response:

Two mistakes I see and have corrected. please find the below program. October is reference variable for the newly created object. so you should use October.pastMonthSpend and another issue is in a constructor, you have to assign value this way pastMonthSpend=spending;

public class Demo123 {
    int pastMonthSpend;

    public Demo123(int spending){
        pastMonthSpend=spending;
    }
    public static void main(String[] args) {

        System.out.println("hii");
        Demo123 October = new Demo123(1433);
        if (October.pastMonthSpend >= 1000) {
            if (October.pastMonthSpend <= 1500){
                System.out.println("Eligible");
            } else {
                System.out.println("Ineligible");
            }
        }
    }
}

CodePudding user response:

"October" is an object instance. You have to access the integer variable "pastMonthSpend" for the comparison. and variable value assigning should be pastMonthSpend = spending; not spending = pastMonthSpend;

  • Related