0

So, for my homework, I have to make a program that allows the user to enter in the highest amount of money they are willing to pay for a house as a whole number. Here's the catch; I have to use if-else statements and JOptionPane dialog boxes.

So I came up with this code:

import javax.swing.JOptionPane;

public class housingDecision {  

    public static void main(String[] args) {
         //listed things that need to be identified
         int houseMoney;
         String input;
         input = JOptionPane. showInputDialog("How much money do you have?");
         //making the input be listed by the user
         houseMoney = Integer. parseInt(input);
         //set up so that if its more than the parameters, itll move on to the next if else statement
         if (houseMoney >= 250000 && houseMoney <= 100000 );
         { 
             input = "You can afford a Townhouse!";
         }
         if(houseMoney >= 250001 && houseMoney <= 400000);
         {
         input = "You can afford a Single Family House!";
         }

         if (houseMoney >= 400001 && houseMoney <= 800000);
         {
        input = "You can afford a Luxury House!";
         }
         if (houseMoney >= 800001);
         {
        input = "Wow! You can Afford a mansion!";
         }          
    }
}

However, it does not run when a whole number is input. What do I need to change so this isn't a problem anymore?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • If a question is either a simple typo, or a list of small issues, it should really be closed until we can clarify the issue and narrow it down. – Carcigenicate Oct 20 '19 at 22:31
  • It's the "spurious semicolon after if-condition" problem. –  Oct 21 '19 at 00:55
  • Possible duplicate of [Semicolon at end of 'if' statement](https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement) –  Oct 21 '19 at 00:56

2 Answers2

0

It seems to run just fine. That being said, it does not achieve the task you are trying to achieve.

  1. You have spaces after . in several of your method calls.
  2. You have no case for if the money isn't within any of the ranges.
  3. You are not printing out the result at all. You're just setting value to the result message and then doing nothing with it.
  4. Your first if statement is broken, the values seem to be swapped.

On top of this, you need to remove those semicolons after the if statements as it isn't resulting in their blocks being called on the condition.

The working code (without the Dialog input), would look like this:

public class Main {
    public static void main(String[] args) {
         int houseMoney;
         String input;
         input = System.console().readLine("How much money do you have? > ");

         houseMoney = Integer.parseInt(input);

         if (houseMoney <= 250000 && houseMoney >= 100000) {
             input = "You can afford a Townhouse!";
         } else if(houseMoney >= 250001 && houseMoney <= 400000) {
            input = "You can afford a Single Family House!";
         } else if (houseMoney >= 400001 && houseMoney <= 800000) {
            input = "You can afford a Luxury House!";
         } else if (houseMoney >= 800001) {
            input = "Wow! You can Afford a mansion!";
         } else {
            input = "You can't afford a house!";
         }

        System.out.println(input);
    }
}
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • 3
    The spaces do not affect how this code is run; it's just an exotic (i.e. discouraged) style. The lack of printing, yeah, now you're on to something. That is one of the many problems with this code. – rzwitserloot Oct 20 '19 at 22:31
  • @rzwitserloot i was totally unaware of the spaces being allowed in Java. That seems crazy. – Nathan F. Oct 20 '19 at 22:40
  • @NilesDoswell if the answer helped resolve the issue for you, consider accepting it :) – Nathan F. Oct 20 '19 at 22:43
0

There are 4 issues in this code:

  1. An if is immediately followed by the statement to run if the clause is true. Just a lone semicolon (;) is itself a statement (the do-nothing statement). Furthermore, in java just braces is also legal, so, in your code, ALL the code runs.

  2. All you're doing is setting the input variable, and that's it. Setting the input variable doesn't magically print things; try System.out.println, for exmaple.

  3. Your first if is broken; you have a 0 too many or too few (the condition asks if the input is both larger than 250k and smaller than 100k; thats of course always false).

  4. Because there is no 'else' clause, if none of the conditions hold, nothing would happen once you fix the other problems.

Fix ALL of those things and your code will work.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72