Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: 2 errors I can't understand

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 2 errors I can't understand

    I am in a Java Programming course and the first assignment was to generate a mortgage calculating java application that will calculate a $200,000 for a 30 year term at 5.75% interest and calculate it into what the monthly interest payment would be. This is what I have so far...
    /**
     * @(#)MortgageCalculator.java
     *
     * MortgageCalculator application
     *
     * @author Brock Overberg
     * @version 1.00 2010/12/19
     */
     
    public class Main {
     
        public static void main(String[] args) {
     
        // Declare, initialize and calculate variables
    long amount = 200000.00; // Total amount of loan
    int term = 360; // Term of loan in months
    int interestRate = 5.75; // Interest rate
    long amount * int interestRate = totalInterestRate; // Multiply total amount of loan by the interest rate
    totalInterestRate / 360; // Total interest rate divided by 360 months in 30 years
    System.out.println("The monthly interest payment will be" = "totalInterestRate" / "360"); // Display the result
    }
    }
    I get 2 errors as follows...

    C:\Users\Brock\Documents\JCreator Pro\MyProjects\MortgageCalculator\src\MortgageCalc ulator.java:18: ';' expected
    long amount * int interestRate = totalInterestRate; // Multiply total amount of loan by the interest rate
    ^
    C:\Users\Brock\Documents\JCreator Pro\MyProjects\MortgageCalculator\src\MortgageCalc ulator.java:19: not a statement
    totalInterestRate / 360; // Total interest rate divided by 360 months in 30 years
    ^
    2 errors

    Process completed.

    Can anyone help?
    Last edited by helloworld922; December 20th, 2010 at 05:25 PM.


  2. #2
    Junior Member jsh5352's Avatar
    Join Date
    Dec 2010
    Location
    I live in California, but not in a good town but affordable.
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: 2 errors I can't understand

    line 18 is in the wrong order: variable = x + y;
    your variable is undeclared with no type. Choose a type int or double? Which is for money?
    5.75 percent is actually .0575 as a decimal number.

    -john

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: 2 errors I can't understand

    New revised version...

    /**
     * @(#)MortgageCalculator.java
     *
     * MortgageCalculator application
     *
     * @author Brock Overberg
     * @version 1.00 2010/12/19
     */
     
    public class MortgageCalculator.java {
     
        public static void main(String[] args) {
     
        // Declare, initialize and calculate variables
    Double totalAmount = 200000.00; // Total amount of loan
    Double termInMonths = 360; // Term of loan in months
    Double interestRate = 0.0575; // Interest rate
    System.out.println("Monthly Interest Rate" = "200000 * 0.0575 / 360"); // Display the result
    }
    }
    Last edited by helloworld922; December 26th, 2010 at 09:59 PM.

  4. #4
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: 2 errors I can't understand

    Here is the proper working code:

    public class MortgageCalculator {
     
    	public static void main(String[] args) {
    		// Declare, initialize and calculate variables
    		double totalAmount = 200000.0; // Total amount of loan
    		double termInMonths = 360.0; // Term of loan in months
    		double interestRate = 5.75; // Interest rate
     
    		// Display the result
    		System.out.println("Monthly Interest Rate: "+ ((totalAmount * interestRate) / termInMonths)); 
    	}
    }
    Hope that helps,
    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  5. #5
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Talking Re: 2 errors I can't understand

    Hey Brock,

    There are some things for you,

    public class MortgageCalculator.java {}
    When we declare a java class we shouldn't mention the ".java" extension inside the source code.
    The rule is that the name of your source file and your only public class should match. It means that if your source file is Hello.java, then your public class name should be Hello.


    Double totalAmount = 200000.00; // Total amount of loan
    Double termInMonths = 360; // Term of loan in months
    Double interestRate = 0.0575; // Interest rate
    Plus, you are using Wrapper classes above, which means "Double". They shouldn't be used unless you need to use primitives as objects, which is not the case here. So you can go with simple primitives "double" declarations.

    System.out.println("Monthly Interest Rate" = "200000 * 0.0575 / 360");
    Now finally coming to your print statement, the concatenating operator in java is '+' and not '=', so you must use '+' whenever you are trying to display your result on console.

    Secondly, you are using hard coded values for calculations. If hard coded values were meant to be used then whats the point of declaring the variables? Keep this in mind, not to go for hard coded values when we have the same initiated inside variables.

    I hope that things are much clear to you now!

    Goldest
    Last edited by goldest; December 21st, 2010 at 01:15 AM.
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  6. The Following User Says Thank You to goldest For This Useful Post:

    JavaPF (December 23rd, 2010)

  7. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 2 errors I can't understand

    I don't know whether to ask this here or not, but because I see its relevant, I am going to ask it anyways. I come from c and am very very new to java however in c we were always taught to refrain from using double for money, instead use int for value then other int for pennies, and i found out that it was much more effective as a system to deal with money. Is it okay in java to use doubles for monetary calculations? Will it be pain in the [bleep] when the problem gets complex?

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 2 errors I can't understand

    Doubles are subject to the same problem in Java or in C/C++.

    It all has to deal with the way floating point numbers are represented.

    When you use an integer/long value, you're guaranteed to keep track of every last digit. Floats and doubles are meant to keep track "rough estimates" of decimal values. The consist of a mantissa and an exponent (similar to scientific notation representation of numbers).

    For example, say there are a billion people and they each have to pay $5 to a pool of money. Now you're goal is to write a program which will add $5 to the pool every time someone contributes.

    For the sake of simplicity, this could be modeled as:
    		float poolF = 0;
    		long poolI = 0;
    		int individualContribution = 5;
    		for (int i = 0; i < 1000000000; ++i)
    		{
    			poolF += individualContribution;
    			poolI += individualContribution;
    		}
    		System.out.printf("float pool amount = $%.2f\n", poolF);
    		System.out.println("actual (integer) pool amount = $" + poolI);

    At a certain point, the floating point representation of 5 dollars becomes so insignificant compared to the current pool amount that the floating point value will just add 0 instead of the 5 dollars. Integers/longs do not suffer from this problem (they will suffer from other problems due to their outer bounds, but for a 32/64-bit system these values are far beyond any monetary values used today)

    This leads to the following output:
     
    float pool amount = $134217728.00
    actual (integer) pool amount = $5000000000




    Using doubles in this above example would remedy this particular case (in fact it would fix most conceivable cases), but at some point it could still run into the same problems.

    Long story short:

    1. Using floats/doubles is ok for most cases (the vast majority of cases) in both C/C++ and Java
    2. Any cases it would not be ok in one means it will not be ok in the other
    3. The syntax of C/C++ and Java are similar enough in this respect that you could almost copy the C/C++ code verbatim and it would work in Java for this type of problem (there are a few sticky points such as pointers and references you'd have to remedy, usually this means the Java code would be easier rather than harder).
    Last edited by helloworld922; December 27th, 2010 at 12:57 AM.

  9. #8
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 2 errors I can't understand

    Helloworld922, what you have wrote is perfect example of the way the data type should be taught in any languages and is very insightful, however one thing I want to point out is that, it seems though the comparison you did was mainly between float and a double, or did I misunderstood(I am not trying to undermine your input)? If I didn't, then would the same comparision be done between a double and an integer when it comes to a large digit number, specially if the other end of the spectrum(i.e. double) has a large precision(i.e. decimal places)?

    I repeat again. I am not being smartass or trolling, am just trying to understand this. However as you have already stated the java and c/c++ are very similar and I concur with that, saying so, I must also say that, was merely trying to see the common programing techniques used in java[if there are any, different than others].

    Thank you

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 2 errors I can't understand

    I'm simply trying to define the scale at which you should be worried about using floats vs. doubles and ints vs. longs.

    For all practical monetary values, you probably won't see these problems with doubles, but they could pop up with floats (albeit, rather large monetary values).

    Take the previous code and run it with doubles (I would advise against trying to run this, it will take a while):

    double poolF = 0;
    		long poolI = 0;
    		int individualContribution = 5;
    		for (long i = 0; i < 100000000000L; ++i) // even increased the number of contributors by 100x
    		{
    			poolF += individualContribution;
    			poolI += individualContribution;
    		}
    		System.out.printf("double pool amount = $%.2f\n", poolF);
    		System.out.println("actual (integer) pool amount = $" + poolI);

    Even after 100 billion contributors, the double result has no error. Also, keep in mind that even with floats the code was able to get up to 1 billion contributors, a rather large amount in itself.
     
    double pool amount = $500000000000.00
    actual (integer) pool amount = $500000000000




    Longs and integers will eventually experience "wrapping error". This happens when the mathematical operation takes the result physically out of the range of longs and integers (2^63-1 for longs, 2^31-1 for integers). In monetary terms, it's possible to overload an integer (~2 trillion), however like with doubles it's nearly impossible to conceive a practical case where longs would be overloaded (~9 sextillion).

Similar Threads

  1. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM
  2. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM
  3. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  4. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  5. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM