Results 1 to 4 of 4
  1. #1
    BriMan83 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default formatting output from array to screen

    Hi everyone,

    This is more a matter of wanting to make my output look clean then anything else, but here is a project I'm working on. It is for a school assignment, but I'm going above and beyond the requirements so I'm hoping no one minds helping me with this problem I'm having.

    Java Code:
    import java.util.Arrays;
    import java.util.Random;
    
    public class ArrayTestApp
    {
        public static void main(String[] args)
        {
            double[] values = new double[99];
            
            for (int i = 0; i < values.length; i++)
            {
                double x = Math.random() * 100;
                values[i] = x;
            }
            
            double sum = 0.0;
            for (double value : values)
            {
                sum += value;
            }
            double average = sum / values.length;
            
            System.out.println("Average value is: " + average + "\n");
            
            Arrays.sort(values);
            System.out.println("Median value is: " + values[values.length/2] + "\n");
            
            for (int i = 8; i <values.length; i += 9)
            {
                System.out.println("The nth value of the array is: " + values[i]);
            }
        }
    }
    The code executes perfectly, but I want to tweak this last section here and I don't know how to do it.

    Java Code:
    Arrays.sort(values);
            System.out.println("Median value is: " + values[values.length/2] + "\n");
            
            for (int i = 8; i <values.length; i += 9)
            {
                System.out.println("The nth value of the array is: " + values[i]);
            }
        }
    It sorts perfectly, and displays the output correctly, but where it currently says nth value, I would love it to actually display the the number of the value it is. As it's pulling every ninth entry I would love it to say "The 9th value is: ......", then the next line would say "The 18th value is: .....". I'm hoping this makes sense. If anyone know a way I could do this, I would greatly appreciate it. If it can't be done, I'll just pull the statement out of the loop, and just have it print the numbers.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: formatting output from array to screen

    Java Code:
    System.out.println("The " + (i+1) + "th value of the array is: " + values[i]);
    ???
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    BriMan83 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: formatting output from array to screen

    Quote Originally Posted by PhHein View Post
    Java Code:
    System.out.println("The " + (i+1) + "th value of the array is: " + values[i]);
    ???
    You are awesome PhHein. I had been trying something like that last night, and for whatever reason it wasn't working for me. I'm thinking I was missing one little symbol somewhere and it was screwing the whole thing up. Your code worked beautifully though. Thank you.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: formatting output from array to screen

    "The 21th value of the array is"...?

    Sorry, just being a bit pedantic.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Formatting Output
    By Rahim2312 in forum New To Java
    Replies: 18
    Last Post: 05-04-2012, 10:45 AM
  2. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 01:10 PM
  3. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 07:43 PM
  4. Formatting output somewhat like a table
    By latereg in forum New To Java
    Replies: 3
    Last Post: 04-06-2010, 07:44 AM
  5. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 05:58 AM

Posting Permissions

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