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: Why is Java not printing my method results?

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

    Default Why is Java not printing my method results?

    I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code. Thanks!

    public static void main(String[] args) { 
     
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter Hours Traveled\t"); 
    int hoursTraveled = input.nextInt(); 
    System.out.println("Enter MPH\t"); 
    int mph = input.nextInt(); 
     
    String output = "Hours\t\tDistance\n"; 
     
    for (int i = mph; i < hoursTraveled; i++) { 
    output += String.format("%s\"\t\t\n", i, distance(mph, i)); 
    } 
    System.out.println(output); 
    } 
     
    public static double distance(int time, int speed) { 
     
    double distance = (time * speed); 
    return distance;
    Last edited by cheshire; April 13th, 2014 at 07:57 PM. Reason: forgot java tag


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Why is Java not printing my method results?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can you copy the console and paste it here that shows what the program does?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Why is Java not printing my method results?

    this is the output if that's what you mean.

    Enter Hours Traveled
    5
    Enter MPH
    40
    Hours Distance
    1
    2
    3
    4
    5

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Why is Java not printing my method results?

    It looks only the value of i is printed. Look at the format String to see why only one value is getting formatted to the output.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is Java not printing my method results?

    I'm trying to figure it out but I'm not sure where the mistake is.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Why is Java not printing my method results?

    How many "%" symbols are in the format String? How many values are you trying to print?
    The counts should match.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: March 1st, 2013, 08:13 PM
  2. Printing from a Method
    By LoganC in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 4th, 2012, 07:33 PM
  3. Printing data from a method
    By LoganC in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 23rd, 2012, 07:51 AM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 11:37 AM