Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 4b technique, pt 2.

 
Paul Ralph
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to ask this without posting my code. Cue the lightning bolts...

> int nameIndex = 1; //cannot be set to 0 or upcoming loop will never be entered
> long tempNumber = 0;
>
> while ( nameIndex > 0)
> {
> tempNumber = number ;
> nameIndex = 0;
So you set it to 1 and immediately reset it to zero. Why not use
while ( nameIndex >= 0 )
in the first place?

Because then the loop will never exit. I want it exit when
( nameIndex == 0) at the end of the loop. I could avoid the funky immediate variable change by using
do
{
//code
} while ( nameIndex > 0)
to make the loop is entered at least once, but this is strictly forbidden by the style guide.

>
> // seperate the first three digits and find correct index for modifier
> while ( tempNumber > 1000)
This loop is run through four times. That doesn't seem like very many. Sometimes when a loop
does this few iterations, it turns
out to be easier to just hard code the four instances.

Actually, if the number is in the billion range it will be used 6 times (3+2+1), 3 for million, once for the thousand range, and none at all if the number is less than a thousand.
Is there a way to
a) Get the first three digits
b) Find the index of the array for the modifier (billion, million, thousand)
without using a loop?
I get the message: Don't use a loop!!! How do I do that without having to completely rewrite my code?
Paul R
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Ralph
I cannot help you much as i am also trying to make my attempt to 4b. But i hope you will get your reply in the following thread:

http://www.javaranch.com/ubb/Forum19/HTML/000531.html

~Rashid
 
Paul Ralph
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it gave me what I needed. At the very least I have another version that's on its way. (Note: I'd read it before, but sometimes it takes more than one reading)
int numberOfLoops = 0; //
int numberOfMethods = 3; // main counts as 1
Shine the light in my face long enough and I'll eventually see it. Now I understand why others have said that BASIC and FORTRAN programming has haunted them. How right they were....
Paul R


[This message has been edited by Paul Ralph (edited May 17, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic