Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help printing specific results from some arrays.

    Here is the code to initialize, populate and process two arrays. It is meant to list the runners in a marathon and their respective times. The result is to find the winner and post their name and time.

    I have finished the code, but need to clean it up. When I execute the code, it is printing all the successive results as it updates the minimum time in the loop.

    What can I do so it executes this process blindly and only prints the final result?

    [/code]
    public class Marathon {

    public static void main (String[] arguments) {
    String[] names = {

    "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",

    "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",

    "Aaron", "Kate"

    };
    int[] times = {

    341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,

    343, 317, 265

    };

    for (int i = 0; i < names.length; i++) {
    System.out.println(names[i] + ": " + times[i]);
    }
    int min = times[0];

    String winner = null;
    for (int i = 1; i < times.length; i++){
    if (times[i] < min){
    min = times[i];
    winner = names[i];
    }

    System.out.println("The fastest runner finished the race in " + min + " total minutes.");
    }

    System.out.println("The winner is " + winner + ".");
    }
    }
    [/code]
    Last edited by Skuby35; 02-16-2011 at 12:12 AM.

  • #2
    Regular Coder Kristofa's Avatar
    Join Date
    Jun 2009
    Location
    Leicester
    Posts
    143
    Thanks
    7
    Thanked 20 Times in 20 Posts
    You have your second println statement still inside your for loop, move it outside so it only prints the once. (See the bit in red.)

    Code:
    public class Marathon {
    
    public static void main (String[] arguments) {
    String[] names = {
    
    "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
    
    "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
    
    "Aaron", "Kate"
    
    }; 
    int[] times = {
    
    341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
    
    343, 317, 265
    
    };
    
    for (int i = 0; i < names.length; i++) {
    System.out.println(names[i] + ": " + times[i]); 
    }
    int min = times[0];
    
    String winner = null;
    for (int i = 1; i < times.length; i++){
    if (times[i] < min){
    min = times[i];
    winner = names[i];
    }
    
    }
    System.out.println("The fastest runner finished the race in " + min + " total minutes.");
    
    
    System.out.println("The winner is " + winner + ".");
    }
    }
    Learning along the way...

  • Users who have thanked Kristofa for this post:

    Skuby35 (02-15-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i could kick myself....thank you!


  •  

    Posting Permissions

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