How to Output Two Columns to the Console in Java

Is there an easy way to output two columns to the console in Java?

Use the width and precision specifiers, set to the same value. This will pad strings that are too short, and truncate strings that are too long. The '-' flag will left-justify the values in the columns.

System.out.printf("%-30.30s  %-30.30s%n", v1, v2);

How do I line up multiple values from System.out.print into columns neatly?

System.out.printf("%40s %d,%n",child.getName(),child.length());

Where 40 is the minimum width. Change this as you see fit.

Edit: using %n instead of \n for OS specific linebreak.

Array that prints something every two columns

Here is an example for you:

public class Main {
public static void main(String args[]) {
String example[] = { "item 1", "item 2", "item 3",
"item 4", "item 5", "item 6", "item 7",
"item 8", "item 9", "item 10" };
for (int i = 0; i < example.length; i += 2) {
System.out.println(example[i]);
}
}
}

output:

item 1
item 3
item 5
item 7
item 9

How to read text file and reformat in only two columns in Java?

Use String.split() to extract the house numbers. Then loop through them to print it (or collect it to a StringBuilder if needed). eg:

           while (sc.hasNextLine()){
String[] tokens = sc.nextLine().split("\\s+");
String houseName = tokens[0];
for(int i=1; i< tokens.length ; i++)
System.out.println(houseName + " " + tokens[i]);
}

How to format string output, so that columns are evenly centered?

I'm also going with the "format" suggestion, but mine is a little different.

In your program, you're relying on your card's toString. So make that formatted in a fixed length, and then you can use them anywhere and they will take up the same space.

public String toString() {
return String.format( "%5s of %-8s", rank, suit );
}

When you print this out, you'll have all your cards aligned on the "of" part, which I think is what you were going for in your first output column.

The "%5s" part right-aligns the rank in a field 5 characters wide, and the "%-8s" part left-aligns the suit in a field 8 characters wide (which means there are additional spaces to the right if the suit is shorter than 8 characters).

How can I print out in columns in java

I would use System.out.printf(...) and use a template String to help be sure that all columns line up. Then you could print things out easily in a for loop.

For example:

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Foo4 {
public static void main(String[] args) {
List<Bar4> bar4List = new ArrayList<>();
bar4List.add(new Bar4("Donald", 3, "A", 22.42));
bar4List.add(new Bar4("Duck", 100, "B", Math.PI));
bar4List.add(new Bar4("Herman", 20, "C", Math.sqrt(20)));

String titleTemplate = "%-10s %6s %6s %9s%n";
String template = "%-10s %6d %6s %9s%n";

System.out.printf(titleTemplate, "Name", "Value", "Grade", "Cost");
for (Bar4 bar4 : bar4List) {
System.out.printf(template, bar4.getName(),
bar4.getValue(), bar4.getGrade(), bar4.getCostString());
}

}
}

class Bar4 {
private String name;
private int value;
private String grade;
private double cost;
private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

public Bar4(String name, int value, String grade, double cost) {
this.name = name;
this.value = value;
this.grade = grade;
this.cost = cost;
}

public String getName() {
return name;
}

public int getValue() {
return value;
}

public String getGrade() {
return grade;
}

public double getCost() {
return cost;
}

public String getCostString() {
return currencyFormat.format(cost);
}

}

Which would return:

Name        Value  Grade      Cost
Donald 3 A $22.42
Duck 100 B $3.14
Herman 20 C $4.47

For more details on the user of the String format specifiers (i.e., the %6d and %6s above), please look at the Formatter API.



Related Topics



Leave a reply



Submit