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 6 of 6

Thread: Calculating time taken - does this look right? Error!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calculating time taken - does this look right? Error!

    /**
    * Method works out time taken to perform an algorithm
    *
    *
    */
    public static void timeTaken() {

    long startTime = System.currentTimeMillis();
    long time = 0;

    for(int i = 0; i < 1000; i++) {
    time += i;
    }

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime); //prints time taken
    }
    This is what the method I have written looks like.
    The error I get is 'void' type not allowed here, which I researched and learnt that: I am using a method that does not return a value in a place where a value is required such as the right side of an equal sign or a parameter to another method.

    The thing is, I don't see exactly where that applies in my code!
    Please help me!

    Thank you for your time and knowledge.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Calculating time taken - does this look right? Error!

    You method is fine. How are you calling it?

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculating time taken - does this look right? Error!

    Quote Originally Posted by PhHein View Post
    You method is fine. How are you calling it?
    I'm calling it from my main method, and I would really like to call it using System.err.println()

    At the moment, I can see that System.err.println(timeTaken()) means that I'll have to change something in the timeTaken method - what and how should I alter it?

    Thank you so much for your reply!

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Calculating time taken - does this look right? Error!


  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Calculating time taken - does this look right? Error!

    hi

    You need to have the method defined as public static long timeTaken() and return a long variable say message. where message = endTime - startTime.
    And in the main method you need to call System.err.println(timeTaken()); and your desired result will be possibly achieved.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculating time taken - does this look right? Error!

    Please copy full text of error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Run time error
    By kmaka in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2013, 07:19 PM
  2. Run Time Error
    By Tanmaysinha in forum What's Wrong With My Code?
    Replies: 22
    Last Post: March 8th, 2012, 11:29 AM
  3. [SOLVED] Calculating when the condition is during a time frame
    By YoungMaker in forum Java Theory & Questions
    Replies: 2
    Last Post: September 28th, 2011, 08:39 AM
  4. Help with run time error
    By white97 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 11th, 2011, 12:35 PM
  5. [SOLVED] Run time error
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 11:05 AM