• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

How can I display text using a single String in the following format within Java?

I need to create a Single string that is able to output in the following two column format and alignment:

Id:                BLUE Vehicle 2
Location:          [307.34, 405.57, 555.26]
Destination:       [490.74, 225.81, 60.92]
Speed:             5.0

Open in new window



How can I accomplish this alignment?
0
Eindoofus
Asked:
Eindoofus
  • 4
  • 3
2 Solutions
 
for_yanCommented:

ArrayList<Double> location;
ArrayList<Double> destination;

String line1 = "";
String line2 = "";
for(int j=0; j<location.size(); j++){
line1 += "\t\t" + location.get(j);
line2 += "\t\t" + destination.get(j);
}

String line = line1 + System.getProperty("line.separator") + line2;

Open in new window





0
 
for_yanCommented:
another way to align in plain test is to count number of characters - this is very simple:


String line = "";

line += location.get(0);

while(line.length() <15)line += " ";

line += location.get(1);

while(line.length() <30)line += " ";

etc.

Open in new window


You can define the array of thiese column lengths - and do it in the loop of course
This worked nicely for me many tiumes

0
 
CEHJCommented:
What form are the data in in the first place?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
EindoofusAuthor Commented:
I also need to find a way to add the correct amount of spaces after Id: and Speed:
0
 
CEHJCommented:
Try
System.out.println(String.format("%-15s", "foo"));
	final String LF = System.getProperty("line.separator");

	String result = String.format(
		"%-15s%s%s%-15s%s%s%-15s%s%s%-15s%.2f", 
		"Id:",
		"III", // Use Id field instead
		LF,
		"Location:",
		"LLL", // Use location.toString instead
		LF,
		"Destination:",
		"DDD", // Use destination.toString() instead
		LF,
		"Speed:",
		Math.PI); // Use speed instead
	System.out.println(result);

======================= prints:

Id:            III
Location:      LLL
Destination:   DDD
Speed:         3.14

Open in new window

0
 
CEHJCommented:
Oops - ignore the first line of code
0
 
for_yanCommented:
I thought you need to align numbers in the lists, otherwise just right number of spaces
will do it:

    ArrayList<Double>location = new ArrayList<Double>();
         ArrayList<Double>destination = new ArrayList<Double>();

        location.add(307.34);
         location.add(405.57);
         location.add(555.26);
        destination.add(490.74);
         destination.add(225.81);
         destination.add(60.92);
       

        String line =    "Id:             Blue Vehicle 2" + System.getProperty("line.separator") +
                "Locations:      " + location +  System.getProperty("line.separator") +
                "Destinations:   " + destination + System.getProperty("line.separator") +
                "Speed:          5.5" +System.getProperty("line.separator") ;

        System.out.println(line);
                

Open in new window


Id:             Blue Vehicle 2
Locations:      [307.34, 405.57, 555.26]
Destinations:   [490.74, 225.81, 60.92]
Speed:          5.5

Open in new window

0
 
for_yanCommented:
If you have different pieces in the beginning of the lines, not just known
strings like "Speed:", etc
then you can use this thing with

while(line.length() < 20)line += " ";

and in this way you can align with any words, just make sure that
it is less than say thie number of characters  long (in my example above less than 20 charcaters long)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now