• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Super NOOB JAVA - Align Output

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
ok anyone who has viewed my other question threads will know this will be a challenge cause my terminology is limited and well basically my experience period is very limited.


ok in my program i have figured out how to use DecimalFormat to get required results for output of decimla places.

my instructor provides an output screen to code to when in doubt. (his instructions are kinda limited and strict as he expects you to read the book and catch on like there is no problem)

Problem - when i use different values from the pic provided the decimal points dont line up .

example:

when i get an output of exact values provided i get this

HTML:
  Management Summary:
  Number of Loans                          2
  Total Loans                      $ 521,600.01
  Interest Earnings                   27,723.35
  Average Interest Rate                    9.473%


now if i enter smaller values i get something like this

HTML:
Management Summary:
Number of Loans                   2
Total Loans                       $ 21,600.01
Interest Earnings                27,723.35
Average Interest Rate          5.47%

is there a way to always have decimal points align no matter the length of number ?
 
I'll start by saying, I don't think it's worth it to spend too much time in figuring out how to get output on your console to be formatted perfectly. In most cases when you are working on a real GUI you will have tools for formatting, and when you are working on a console you are mainly debugging.

That's not to say you don't need to think about it, and it's of course good to understand how you can get new rows, tabs etc.

Since there is no source code provided, I'll show a quick hack written in a way that you could hopefully understand it. The basic idea:

- First you would need to know the longest number of characters before the decimal point. In your first example that would be 9 for "$ 521,600", in the second one it would be 8 for "$ 21,600". To obtain this value all of the variables have to be examined, so I put them in a list for faster looping.

- When printing out the value you can then add empty spaces as padding so they align by the decimal point.

Here is the example that requires you create the list in your own program:

Code:
public class OutputFormatter {

    public static int getLongestPrecedingChars(List<String> values) {
        int longestPrecedingDecimal = 0;

        for (String value : values) {
            String[] splitValue = value.split("[.]");

            if (splitValue[0].length() > longestPrecedingDecimal) {
                longestPrecedingDecimal = splitValue[0].length();
            }
        }
        return longestPrecedingDecimal;
    }
    
    public static String addPadding (String value, int longestValue) {
        int paddingSize = longestValue - value.split("[.]")[0].length();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < paddingSize; ++i) {
            stringBuilder.append(" ");
        }
        stringBuilder.append(value);
        return stringBuilder.toString();
    }  
}

The methods are static, so you can just call them directly from your own class, like:

Code:
int longestPreceding = OutputFormatter.getLongestPrecedingChars(values1);
for (String value : values1) {
            System.out.println(OutputFormatter.addPadding(value, longestPreceding));
        }

I made a quick test method, which seemed to align correctly:

Code:
public static void main (String args[]) {
        List<String> values1 = new ArrayList<String>();
        values1.add("2");
        values1.add("$ 521,600.01");
        values1.add("27,723.35");
        values1.add("9.473%");

        List<String> values2 = new ArrayList<String>();
        values2.add("2");
        values2.add("$ 21,600.01");
        values2.add("27,723.35");
        values2.add("5.47%");

        int longestPreceding = OutputFormatter.getLongestPrecedingChars(values1);
        for (String value : values1) {
            System.out.println(OutputFormatter.addPadding(value, longestPreceding));
        }

        longestPreceding = OutputFormatter.getLongestPrecedingChars(values2);
        for (String value : values2) {
            System.out.println(OutputFormatter.addPadding(value, longestPreceding));
        }
    }

Which prints:

Code:
        2
$ 521,600.01
   27,723.35
        9.473%
       2
$ 21,600.01
  27,723.35
       5.47%

To make it more easy to use (but more complicated in itself), you could adapt from that so that:

- The methods are not static
- You have some global variable which is a map or list of String arrays containing all entries till that point
-- array[0] is the description
-- array[1] is the value
- you have another global variable which contains the longest entry to this point (like in the example above)

- constructor initializes the global variable
- you have a method you can call from your code like:
Code:
addToQueue(String description, String value);
-- which adds the values to the list
-- checks if the "longest preceding" value needs to be increased

- And another method like
Code:
flushQueue();
Which does the same as I did above, but for the stored values.

Using it would then just be as simple as:
Code:
OutputFormatter outputFormatter = new OutputFormatter;
outputFormatter.addToQueue("Number of Loans", "2");
outputFormatter.addToQueue("Total Loans", "$ 521,600.01");
...
outputFormatter.flushQueue();

Note, in my first example above I'm not taking into account the description you are printing, in the latter example you would have to include it.
 
Back