1. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

Hey everyone here at Java Forums. Im a beginner to program but I really want/need to learn how to do it so I thought this would be the best way!

Basically i need to write a java program that prints the table of sines from 0 degrees to 90 degrees. The left side column is the angle and has to be justified in a field width of 2 and the right has to have three decimal places.

ALSO, there has to be a space every 10 lines... of example, after 9, 19, 29.....

So I'm obviously not asking you guys to write me the code, what i have so far is this... I wrote a for loop
for (i=0; i<=90; i++)
and i know i have to use the printf method to display the correct format. I keep getting an error however when i try to use

System.out.printf("%2f %3.3f" i, Math.sin(i));

Lastly, I believe i have to use the modulus operator to put the 3 spaces every 10 lines... Im just really confused on how to do that. My guess is that if the remainder is 0, then put a 3 spaces?

ANY help would be greatly appreciated.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

I keep getting an error however when i try to use

System.out.printf("%2f %3.3f" i, Math.sin(i));
What is that error?

I believe i have to use the modulus operator to put the 3 spaces every 10 lines... Im just really confused on how to do that. My guess is that if the remainder is 0, then put a 3 spaces?
Try that. If it does not result in the output you described, post the code and a description of what does happen.

3. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

What i have is

for (i=0; i<=90; i++)
System.out.printf("%2f %3.3f\n", i, Math.sin(i));

and it says cannot find symbol variable i...

When i declare int i; before the for loop that error message goes away but then when i debug i get the error:
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer

You have to declare it and initialize it. You can do this directly in the for loop, or outside of it

Inside loop:
Java Code:
`for(int i = 0; i < 100; ++i)`
outside loop:
Java Code:
```int i=0;
for(i = 0; i < 100; ++i)```

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

The error you get when you declare i is saying that %f is not appropriate for an int: try %d instead. (d for decimal.)

6. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

Thanks a lot for the responses guys. I have this so far and still won't compile... is my logic wrong? or is it just my syntax?

for(int i = 0; i < 100; ++i)
System.out.printf("%2.0d %6.3d%\n", i, Math.sin(i*(Math.pi/180)));
if (i%10==9) System.out.println();

7. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

OH man I'm SO CLOSE!

here's what i have

for(double i = 0; i <= 90; i++)
if ( ((i %10)+1) == 0)
System.out.println();
else
System.out.printf("%2.0f %6.3f\n", i, Math.sin(i*(Math.PI/180)));

but it prints out for example
78
79

81
and skips the 80th value! Any idea on how i can make it show the 10th value so its like
7
8
9

10

8. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

Alright finally got it to work. Thanks to all who helped =]!