Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help need on math java program

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help need on math java program

    Hi I am trying to write a java program that lets me evaluate the value of x over the range x=0 to100, for the function y=a*(x*8))+b*((x*8)^-0.5))+c and then print the values of y into a file. However the attempt I made below does not work can someone help me out and show me where I am going wrong and what to do to make the code work.

    public class javatest {
     
    public static void main(String[] args) {
     
    // Stream to write file
    FileOutputStream fout;
     
    try
    {
    // Open an output stream
    fout = new FileOutputStream ("output.txt");
     
    double a = 5;
    double b = 10;
    double c = 15;
    double y;
    double x;
    y=(a*(x*8))((b)*Math.pow((x*8)),-0.5)+c;
     
     
    // for x returns y for 100 channels.
    for (x=0; x < 100; x+) {
    System.out.println(
    "The value of y is " y;
     
    // Print a line of text
    new PrintStream(fout).println (" The value of y is" y;
     
    // Close our output stream
    fout.close();
     
    }
     
    }
     
    }
    Last edited by Json; June 30th, 2010 at 03:13 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help need on math java program

    See post on other thread.

  3. #3
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Help need on math java program

    If you're serious about this, try to compile and at least try to fix the errors yourself, otherwise get back here and post the errors too.

    Hint : I can see one error there which should yell a LOT when you try to compile that.

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help need on math java program

    You've left a lot of mistakes as far as syntax is concerned; fix those or at least compile and see :S.
    For the logic of your program, i guess you'll want to put the equation in that for loop.
    Programmer - an organism that turns coffee into software.

  5. #5
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Help need on math java program

    To give a indication of what the others mean, I saw atleast 7 mistakes in syntax that should give huge errors.

  6. #6
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help need on math java program

    very obvious syntax errors

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Re: Help need on math java program

    My guess is this is your first program, so I'll help out as much as I can.

    Here are some problems you may be facing:
    1) Have you imported FileOutputStream and PrintStream?

    import java.io.FileOutputStream;
    import java.io.PrintStream;

    2) You attempted to create a TRY block, but you failed to take account of an error occuring. After the block, you need to include either a CATCH or FINALLY block. I will include an example of a CATCH block since that is the simplest and most useful for your situation.

    CATCH Block: "catch(Exception e){System.out.println(e);}"

    3) You did not initialize the variable "x". Give it a value before you attempt to use it in the formula.

    4) You incorrectly attempted to use Math.POW(double d1,double d2).

    Your wrote: "((b)*Math.pow((x*8)),-0.5)"
    While it should be: "((b)*Math.pow((x*8),-0.5))"

    Always make sure your brackets are in the correctly place. I also believe you have forgotten a plus ( + ) if the formula in your description is correct.

    5) You incorrectly attempted to print out your results.

    Your wrote: "System.out.println("The value of y is " y;"
    While it should be: "System.out.println("The value of y is "+y);"

    Your wrote: "new PrintStream(fout).println (" The value of y is" y;"
    While it should be: "new PrintStream(fout).println (" The value of y is "+y);

    Keep in mind to include a bracket to close the println() method and keep in mind that to add a number to a string, you have to close the string, include a plus ( + ) and then include the number you want to add. It also doesnt hurt to include a space after "is" so there will be a space between "is" and the number during the output.

    6) Lastly, you have forgotten an ending bracket ( } ). Put one at the end or where you are wanting to end a code block.

Similar Threads

  1. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  2. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  3. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM
  4. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM