• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in using BigDecimal values in for Loop

 
Nagendra Batchu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

BigDecimal bd5=new BigDecimal("89999999999994552010");
BigDecimal bd6=new BigDecimal("89999999999994552020");

how to iterate it from starting bd5 to end of bd6

for(BigDecimal bd51=bd5;bd5.max(bd6);bd51.add(one)){

}

i had used this which is not working ... could any one help me in solving this problem
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this code:


your code is wrong in multiple ways:

1. bd5.max(bd6) returns BigDecimal, it does not compare them, you can compare them with bd51.compareTo(bd6)
2. you cannot use bd5 in the comparison, unless you change bd5 or bd6. Otherwise you end up with infinite loop. Though you can change them inside the body of the loop somewhere, then you good to go with this comparison (remember to use compareTo() , not max())
3. BigDecimal is immutable, so bd51.add(BigDecimal.ONE) returns new BigDecimal, but does not change the bd51 unless you assign it a new value yourself.
 
Nagendra Batchu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...!!!
 
Nagendra Batchu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should only use integer numbers as the counter in a for loop. I presume this example was justified under the heading of "I just wanted to see what happens if . . ."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic