Results 1 to 8 of 8
  1. #1
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Wink New to Java, Please help me learn!

    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.
    Thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: New to Java, Please help me learn!

    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. #3
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: New to Java, Please help me learn!

    Thank you for your reply.

    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

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: New to Java, Please help me learn!

    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. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: New to Java, Please help me learn!

    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. #6
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: New to Java, Please help me learn!

    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. #7
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: New to Java, Please help me learn!

    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. #8
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: New to Java, Please help me learn!

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

Similar Threads

  1. Looking to learn Java.
    By Pwhheee in forum New To Java
    Replies: 6
    Last Post: 10-08-2014, 07:07 PM
  2. learn java
    By bishwa bhushan in forum New To Java
    Replies: 1
    Last Post: 09-04-2011, 10:59 PM
  3. Should I learn C++ first before Java?
    By silverglade in forum New To Java
    Replies: 9
    Last Post: 05-26-2011, 12:58 AM
  4. How do you learn java??
    By low224 in forum New To Java
    Replies: 1
    Last Post: 12-07-2008, 09:42 PM
  5. How to learn java??
    By superroach in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 08:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •