• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck in a loop  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I was wondering if someone can suggest to me a solution for my problem. I know what the problem is just don't know of a method on how to fix. The problem i believe is that I have the max value set to the first value given out, but every time the loop repeats the Max value gets reset to the next value in the file.

Essentially the program is supposed to read a list of credit card transactions and tell the user which transaction is the highest.

here's the list of the sample file, note that the number of transactions could change that's why i haven't tried simply passing the values to method.

Bicycle 562.90
Groceries 138.43
Hotel 612.00

I think I pretty much have everything set up correctly just this one part is confusing me
--------thanks









 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of things wrong.

Line 20 is wrong. You set Max to Dvalue, the value from the line that you've just read.
The if-statement in line 21 will ofcourse always be true, because you've just set Max to be equal to Dvalue.
In line 23 you're doing the exact same thing as in line 20.
In line 24 you print Dvalue. But you do this for every line in the file, since it's all inside the while loop.

you should do this:

Initialize Max to zero before the while loop.
Remove line 20.
Line 24 should be at the end of the program, outside the while loop, because you want to print that once, after all lines have been read, and not for every line. Also, it should print the value of Max, not Dvalue.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is line 20. You always set Max to current value. Don't do that.
Execute Max = Dvalue only if the test passes (inside if).
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at these two lines of code:



Notice that I've removed the misleading comments from that code. Now, when is the boolean condition in the second line going to be false? When answering this, don't pay any attention to the code you have already written and ignore the fact that these two lines come from there. Just look at those two lines of code and tell me when the boolean condition will be false.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Have a look at these two lines of code:



(...) Now, when is the boolean condition in the second line going to be false?


Sir! I know! I know! ;) When Dvalue is Double.NaN
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I got it to work
thanks to everyone, especially Jesper

But yeah the only reason I did the whole max=Dvalue was because I didn't initially initialize the max to zero.
So I wanted to give it a value to compare to. But I'll make sure not to forget to try it in the future
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one more thing -- you'll only want to initialize Max to zero if you expect all of your transactions to be positive numbers ("debits" in accountant-speak). If your list might include negative numbers ("credits") you should initialize it to minus infinity. (Yes, really; check out the Double class because there's a constant in it for minus infinity!)
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing. Don't be tempted to use Double.MIN_VALUE. It might not be what you'd expect it to be. Always read javadoc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!