A real estate office handles, say, 50 apartment units. When the rent is, say, \$600 per month, all the units are occupied. However, for each, say, \$40 increase in rent, one unit becomes vacant. Moreover, each occupied unit requires an average of \$27 per month for maintenance. How many units should be rented to maximize the profit.
Write the program to prompt the user to enter:

Enter the number of apartments

Enter the rent for each apartment

Enter maintenance expense for each rented apartment

Enter the amount of rent increase per apartment

I'm not getting any errors, but the program is not displaying any ouput, what did i do wrong?

``````import java.io.*;
import java.util.*;

public class lab9_2 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter number of apartments");
double no_of_apartments;
no_of_apartments=in.nextDouble();
System.out.println("Enter rent of each apartment");
double rent_per_apartment;
rent_per_apartment=in.nextDouble();
System.out.println("Enter maintanence expense for each rented apartment");
double mApts;
mApts=in.nextDouble();
System.out.println("Enter the amount of rent increase per apartment");
double rInc;
rInc=in.nextDouble();
double rApts;
rApts=in.nextDouble();
double maxprofit=0.0;
double maxNOofunits=0;
double noOfrentUnits = 0;
while(noOfrentUnits==0){
double vacantunits;
vacantunits=mApts-noOfrentUnits;
double totalrentincrease;
totalrentincrease=vacantunits*rInc;
double totalrent;
double maintanenceExpense;
maintanenceExpense=noOfrentUnits*mApts;
double profit;
profit=totalrent-maintanenceExpense;
if(maxprofit<profit){
maxprofit=profit;
maxNOofunits=noOfrentUnits;
System.out.println("Maximum number of units to rent:"+maxNOofunits);
System.out.println("Maximum profit:"+maxprofit);
maxNOofunits++;
maxprofit++;

}//end while loop
}
}//end main
}//end class``````

You have a while loop that does while (noOfRentUnits is 0). But you never set noOfRentUnits to anything other than 0. Furthermore, you said profit = total rent - maintenance expense. But you defined total rent as noOfRentUnits * whatever, and you defined maintenance expense as noOfRentUnits * whatever. Both total rent and maintenance expense will be 0 (according to the equations you have), therefore, profit will also be 0. If profit is 0, and maxprofit is 0, then (this is referring to your if statement) maxprofit is NOT < profit. So basically, you're just repeatedly going through the while loop, but it isn't printing anything because you never set noOfRentUnits to a value, and the if statement never gets executed.

I'm posting the code that I explained above so it will be more clear to you what I'm talking about:

``````double noOfrentUnits = 0;
while(noOfrentUnits==0){
maintanenceExpense=noOfrentUnits*mApts;
profit=totalrent-maintanenceExpense;
if(maxprofit<profit){
//blah blah blah
}
}
}
}``````

As you can see from my explanation above, and from the code segment, it doesn't appear that noOfRentUnits is being set properly, and therefore, all the other equations seem to be wrong, causing the while loop to continuously be true, and the if statement to never get called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.