0

I am a beginner in java. My current assignment is to turn a previous code, into one that uses a GUI. Here is the previous code.

public class Tax {

public static void main(String[] args) {


       //Variables
       double RealHouseValue;
       double AssessedValueRate;
       int TaxDistrict;
       double AssessedValue;
       double PropertyTaxRate;


       //Insert applicable numbers
       RealHouseValue = 300000;
       AssessedValueRate = .35;
       TaxDistrict = 200;
       AssessedValue = RealHouseValue*AssessedValueRate;
       PropertyTaxRate = .03;


       System.out.println("Actual House Value: $" + RealHouseValue); //Displays current house value
       System.out.println();
       System.out.println("Assessed Value Rate is: " + AssessedValueRate + " for tax district: " + TaxDistrict);
       System.out.println();
       System.out.println("Assesed Value: $" + AssessedValue);
       System.out.println();
       System.out.println("Local Property Tax Rate: " + PropertyTaxRate);
       System.out.println();
       System.out.println("Property Tax Due This Year: $" + AssessedValue*PropertyTaxRate);               

What I want to do is have a box ask for the users actual house value, or "E" to exit. When the user inputs their house value, I want it to do the above calculation and output their property taxes due for the year. If they press "E", I want to print out "Good Bye", and stop running. Here's what I've got so far:

import javax.swing.JOptionPane; // Needed for the Scanner class

public class taxGui {

public static void main(String[] args) {


    String custInput;
    double realHouseValue;
    double AssessedValueRate;
    double AssessedValue;
    double PropertyTaxRate;


       //Insert applicable numbers
      realHouseValue = 0.0;
      AssessedValueRate = .35;
      AssessedValue = realHouseValue*AssessedValueRate;
      PropertyTaxRate = .03;

       custInput = JOptionPane.showInputDialog("Enter your house value, or press 'E' to exit.");
       realHouseValue = custInput.charAt(0);

       if (realHouseValue > 0)
       {
           JOptionPane.showMessageDialog(null, "Your property tax due this year is: " + AssessedValue*PropertyTaxRate);
           realHouseValue= custInput.charAt(0);
       }


}

}

I am trying to get the realHouseValue to equal user input instead of 0.0 as declared. I have had a hell of a time with this class, Intermediate Object Oriented Programming. I have tried asking my professor questions, but his answers go right over my head. Any help would be appreciated. Sorry if the formatting or anything is off, this is my first post. Thank you for looking!

-Charles

Charles
  • 1
  • 2
  • Have a look at the answer posted [here](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – d33j Jun 12 '17 at 05:05
  • So you want to build a real GUI like Swing? Or are you looking for a console-based solution? – Nico Van Belle Jun 12 '17 at 06:36
  • Just the pop up windows that is brought up with this import: import javax.swing.JOptionPane; – Charles Jun 14 '17 at 00:59

1 Answers1

0

There are at least a couple of problems that I see in your code:

  1. realHouseValue = custInput.charAt(0);

The above line takes the first letter from the user's input and considers that to be the realHouseValue. Is that your intention?

What you should do is use Double.parseDouble(realHouseValue); to get the actual value that the user is entering.

To cancel the operation, the user can simply click on the Cancel button on the input dialog. So you will get a null as the return value assigned to custInput. You can simply compare this value to "exit" instead of comparing to "E".

  1. AssessedValue = realHouseValue*AssessedValueRate;

The above assignment occurs even before you accept the user's input. So it will always show a non-changing value that is initialized when your program starts. Change this to a function/method such as this:

double calculateAssessedValue(double realHouseValue, doubld AssesedValueRate) {
    return realHouseValue*AssessedValueRate;
}

This can then be called to output it to the user:

JOptionPane.showMessageDialog(null, "Your property tax due this year is: " + calculateAssessedValue(AssessedValue, PropertyTaxRate));

See if you can correct these bugs to get you in the right direction.


Hope this helps!

anacron
  • 5,733
  • 2
  • 21
  • 31