What is the equivalent of the "setw" stream manipulator for C++ in Java?

I've been messing with the java.util.formatter class, but haven't figured this out yet?
http://www.cplusplus.com/reference/iostream/manipulators/setw/

Recommended Answers

All 2 Replies

Generally it's the number between the percent sign ('%') and the letter. ...much as you would see in the C 'printf' function.

StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb, Locale.US);
		formatter.format("/%10d/", 23421);
		System.out.println(sb.toString());

Produces this output: / 23421/

Or you could directly use C style printf method.

System.out.printf("|%10d|",23421);
commented: Good answer. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.