Results 1 to 2 of 2
  1. #1
    jmartin99 is offline Member
    Join Date
    Mar 2017
    Location
    Southern IL
    Posts
    3
    Rep Power
    0

    Default Java output formatting question

    I am in a programming class and I have to write a script to fill an array with 10 random numbers between 1 and 20. I then have to copy them to a new array and then sort that array. Then I have to print both arrays side by side. I have written the script to do this. The problem is if the value of the first array is a single digit, the value of the second array does not fall in line with the other elements of the array

    Example:

    Unsorted Array Sorted Array
    12 1
    16 4
    4 8
    19 10
    1 12

    How can I format it so the sorted array is all in line?

    Thanks!

    Jeff

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java output formatting question

    Look at using the printf() method which will allow you to pad printed values with leading spaces.
    Also the String class's format method will help.
    See the Formatter class for how to build a format string.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help formatting output
    By codyjava in forum New To Java
    Replies: 6
    Last Post: 09-21-2013, 07:34 PM
  2. Formatting output string in Java GUI?
    By dojob in forum New To Java
    Replies: 3
    Last Post: 09-17-2013, 07:16 PM
  3. Formatting Output
    By Rahim2312 in forum New To Java
    Replies: 18
    Last Post: 05-04-2012, 09:45 AM
  4. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 12:10 PM
  5. Replies: 2
    Last Post: 07-02-2010, 02:20 PM

Posting Permissions

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