# Need help with a loop

• 12-01-2008, 10:48 AM
ReV13
Need help with a loop
Hi, Im pretty inexperienced with java and have run into a bit of a problem.

Basically i need to make a loop starting with -10 that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.

thx
• 12-01-2008, 11:08 AM
miss.meli
Quote:

Originally Posted by ReV13
that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.

I understand how you want to loop to go from -10 to 10 but I think I'm still confused on how you want it incremented.

Meli
• 12-01-2008, 11:09 AM
Eranga
You say that you want to print decimal values?
• 12-01-2008, 11:33 AM
beginner21
right,
i can't understand what you want to achieve..
• 12-01-2008, 12:02 PM
serjant
Maybe you meant this:
Code:

```              double num=0.1D;                 for(int i=-10;i<=10;i++){                         System.out.println(num);                         num+=0.01D;                                         }```
• 12-01-2008, 01:23 PM
Eranga
There are lots of ways to do that, but not much clear what our thread starter wants.
• 12-01-2008, 02:15 PM
ReV13
Sorry, I tried to be as clear as I could but im kinda new to java terminology (english isnt my native language eighter):P

Anyways after being stuck for a week, i finally came up with this :

Quote:

int num = -1;
ArrayList <Double> v = new ArrayList<Double>();

for(double n = 0.1; n < 5;n = n + 0.01 ){
}

for (double i = -10; i <= 10; i = i + v.get(num)) {
num = num + 1;
System.out.println(i);
}
any ways this could be done simpler?
• 12-01-2008, 03:00 PM
CJSLMAN
hhhmmm...
Let's see...
• The first for statement is to populate 5 elements in the v arraylist
• The second for statement... I have no clue what this is supposed to do. To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
• What do you want to do with the second for statement? Print the values of the arraylist?

Luck,
CJSL
• 12-01-2008, 03:10 PM
ReV13
dno, the code works perfectly in eclipse
• 12-01-2008, 03:14 PM
beginner21
Code:

```for (double i = -10; i <= 10; i = i + v.get(num)) { num = num + 1; System.out.println(i); }```
what does the second for loop prints?
• 12-01-2008, 03:28 PM
ReV13
The first loop is to make a "step" to get the second loop running properly. Second one is to print out the actual values I need(-10; -9.9; -9.790...01; -9.670...02; etc.)
• 12-01-2008, 03:29 PM
beginner21
is your program prints what you want?
• 12-01-2008, 03:30 PM
CJSLMAN
Let's see...
• You don't know what the second for loop's task is
• ... and you say it's working perfectly... huh... then what is this post for?

If you want help, please explain the problem you're having, because it would appear that you want help for something that is working correctly and you don't have a clue as to how it works.

CJSL
• 12-01-2008, 03:44 PM
CJSLMAN
take something back...
Independently of the questions and doubts stated in the above posts, I would like to correct a statements that I made:
Quote:

To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
The code would probably compile, but would it fail at run time... and the program is the one that would upchuck.
Sorry if this caused any misunderstandings (haven't had my morning coffee yet).
Clarification: the above questions/doubts still stand.

CJSL
• 12-01-2008, 05:25 PM
ReV13
Basically, I managed to come up with a working (tho crude) solution to my problem after I made this thread.

I was just wondering if I could simplify the code. Thx for the help.
• 12-01-2008, 05:48 PM
CJSLMAN
Code:

`int num = -1`
Why is int Num initialized to -1? Try making it equal to zero.. that may be what you are looking for.

Luck,
CJSL