aspose file tools*
The moose likes Beginning Java and the fly likes !!!=====Loop troubles=====!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "!!!=====Loop troubles=====!!!" Watch "!!!=====Loop troubles=====!!!" New topic
Author

!!!=====Loop troubles=====!!!

Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
Hi everyone,

Working on a program to figure depreciation tonight. The following code is to print a chart. I go through the logic, and it seems to do what I want, but I'm not getting the output I expect.

Example output where the original cost is 200000, the final value is 50000 and I depreciate for 5 years.

Years Depreciation
_____________________________

1 50000
2 40000
3 30000
4 20000
5 10000

I think my problem may be with braces, but I've tried every combination to no avail. At one point, I was getting 1-5 and 0.00 for depreciation. I've made changes to resolve other issues and now get nothing but the heading.
Thanx in advance for any suggestions!


[ December 01, 2004: Message edited by: Tricia Lemay ]
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,

Let's imagine that we are the computer compiling this method with the data you suggest.

Example output where the original cost is 200000, the final value is 50000 and I depreciate for 5 years.



If you have defined all of the variables in your class somewhere, then from inside your method you did not assign your method parameters to anything in your method. for example

The following method will not print x, but it will print myX.


This method will print myX;




My suggestion: adjust your code, successfully compile and if you have further trouble -> Post your question with more code and also include actual output.

Cheers!



Jimmy Die
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
Thanx Jimmy! The "my" variables were prsvate variables I had defined. I switched them out and compiled (along with a closing brace). I got most of the output as expected. But it is double counting "n" somewhere because it only runs 1, 2, 3 and stops. I tried other numbers of years and it basically prints half the number of responses. Here's more code... thanx, you've helped a lot already and I really appreciate it.


[ December 02, 2004: Message edited by: Tricia Lemay ]
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
This is the output:

Please enter the original value: 200000
What is the final value of the product? 50000
What is the number of years you wish to depreciate the product? 5

Years Depreciation
_________________________
1 50000.0
2 40000.0
3 30000.0

Do you want to continue? y/n
n
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,


That helps. Now what I do in this situation is to put "break" statements in supicious places. For example.

Inside your while loop but outside your if statement put. System.out.println("Break1");

Then inside your while loop and inside your if statement put:
System.out.println("Break2");

Does it print the number of "Break"'s that you expect. If not write some System.out.println(some of your variables);

This way you will see how your loop are responding and what values your variables are looking like at certain point of your program. (Delete the "break" statements when everything is peachy)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You may also find it helpful to "run" your program by hand. You can make a table with all of your variables and go through the program line by line. At each line, fill in the values of all the variables. This is a great tool to help you see what your program is doing. You can also add System.out.println() calls to display the actual values of each variable in order to verify that they have the values you expect (from the table you created by hand). If you are using an IDE, you may also want to learn how to use its debugger. A debugger typically lets you step line by line through some code and display the values of specific variables.

Finding problems in code is a skill in and of itself, so just like writing the program in the first place, it takes practice. Good luck with this.

Keep coding!

Layne


Java API Documentation
The Java Tutorial
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
Thanx for the input guys I'm gonna keep working on it and try what you suggest. I know it's something stupid that I am overlooking because I did have the count working correctly at one point. Fiddling too much with it I think!
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
Why do you have this line of code:

Shouldn't it be:

as myYearsDepreciated is an int??? I don't know if that will fix your problem, but it could be causing your loop to not drop into the If statement.

EDIT:
This is the problem:
if yearsDepreciated (yd from now on in my post) = 5, and:


then:
loop 1: yd = 5 n=1
loop 2: yd = 4 n=2
loop 3: yd = 3 n=3
yd = n so no more loops.

you only need to count up n, or count down yd...not both
[ December 02, 2004: Message edited by: Logan Owen ]
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
Hi guys,

Just wanted to pass on an update. I stepped through the problem incrementally. I was using yearsDepreciated and changing it - then trying to compare it. When yearsDepreciated got halfway through it equalled out. I created a local variable for the numerator and maintaind the integrity of yearsDepreciated. It may not be "correct" or pretty..... but I get the output I want.

I think I need the last piece of code discussed. First I need to chart the years (1, 2, 3, 4, 5) and then I need to decrement in the numerator of the formula (n, n-1, n-2 etc). I don't know any other way to do that.?

I really want to thank all of you on the forum. Often I will see posts where people want the work done for them. I've learned more from being pointed in the right direction and continuing on from there than someone sitting down and doing my work for me. The explanations I've received here have enabled me to acheive a higher understanding of the language (as little that understanding is!)Thanx for being out there!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: !!!=====Loop troubles=====!!!
 
Similar Threads
Doubt in Math class
please help figure out why the standard deviaion is not giving the correct output
please explain the program
incompatible types: found double required float error;
the world's smallest number?