You can try the below example. Do use ‘-‘ before the width to ensure left indentation. By default they will be right indented; which may not suit your purpose.
System.out.printf("%2d. %-20s $%.2f%n", i + 1, BOOK_TYPE[i], COST[i]);
Format String Syntax: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
Formatting Numeric Print Output: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
PS: This could go as a comment to DwB’s answer, but i still don’t have permissions to comment and so answering it.
Related Posts:
- Double % formatting question for printf in Java
- Print a boolean value with printf
- Java printf formatting to print items in a table or columns
- Could not reserve enough space for object heap
- What does “Could not find or load main class” mean?
- Exception in thread “main” java.util.NoSuchElementException
- “Error occurred during initialization of VM; Could not reserve enough space for object heap” using -Xmx3G
- What’s the simplest way to print a Java array?
- javax vs java package
- What is a stack trace, and how can I use it to debug my application errors?
- How to fix “A JNI error has occurred, please check your installation.”
- Java Runtime Environment not found error when I open an application
- “PKIX path building failed” and “unable to find valid certification path to requested target”
- What exactly does a jar file contain?
- Java optional parameters
- How to update a value, given a key in a hashmap?
- How to schedule a periodic task in Java?
- typeof in Java 8
- Switch on Enum in Java
- error upon assigning Layout: BoxLayout can’t be shared
- Why do this() and super() have to be the first statement in a constructor?
- Else without if
- Keytool is not recognized as an internal or external command
- Cannot find “Package Explorer” view in Eclipse
- Error: Could not find or load main class in intelliJ IDE
- Java Hashmap: How to get key from value?
- What’s the difference between import java.util.*; and import java.util.stream;?
- Collections sort(List
,Comparator super T>) method example - Login Application with 1 stage and multiple scene in JavaFX
- Handle mouse event anywhere with JavaFX
- restart console on Minecraft Server
- SSL peer shut down incorrectly in Java
- Java split string to array
- How can I clear or empty a StringBuilder?
- Maven error : Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher
- Polymorphism vs Overriding vs Overloading
- Difference between Static methods and Instance methods
- Java The method is undefined for this type
- Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio
- Javamail Could not convert socket to TLS GMail
- java.net.SocketException: Connection reset
- Cannot instantiate the type Queue. Why is this?
- Pause the timer and then continue it
- Converting String Array to an Integer Array
- How to fix “unsupported class file major version 60” in IntelliJ?
- Resize an Array while keeping current elements in Java?
- Access restriction: The type ‘Application’ is not API (restriction on required library rt.jar)
- Assign the negativeCntr with the number of negative values in the linked list
- intellij incorrectly saying no beans of type found for autowired repository
- Web server failed to start. Port 8080 was already in use. Spring Boot microservice
- Syntax error on token(s), misplaced construct(s)
- JAVA Variable declaration not allowed here
- How to check if object is null or not except == null
- How do I check if the user is pressing a key?
- How to check if object is null or not except == null
- Comparing strings by their alphabetical order
- exposed beyond app through ClipData.Item.getUri
- Java – get the current class name?
- Warning: The method assertEquals from the type Assert is deprecated
- koch snowflake java recursion
- Java word count program
- How to return a boolean method in java?
- Java read file and store text in an array
- “The import org.springframework cannot be resolved.”
- How to synchronize or lock upon variables in Java?
- why f is placed after float values?
- Syntax error on token “;”, { expected after this token in Random string creator
- Chrome Error: You are using an unsupported command-line flag: –ignore-certifcate-errors. Stability and security will suffer
- Printing char arrays in Java
- How to change already compiled .class file without decompile?
- Java: How To Call Non Static Method From Main Method?
- What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?
- How to “wait” a Thread in Android
- How to use regex in String.contains() method in Java
- Error: Module not specified (IntelliJ IDEA)
- Checking letter case (Upper/Lower) within a string in Java
- How to swap String characters in Java?
- Exception in thread “main” java.net.ConnectException: Connection refused: connect Socket Programming Java
- Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
- Resource leak: ‘in’ is never closed
- JavaFX and OpenJDK
- incompatible types: void cannot be converted to int [duplicate]
- Long cannot be dereferenced
- Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven
- Regex pattern including all special characters
- Error: ‘)’ expected compiler error
- How does addNotify() and requestFocus() work in Java with JPanel?
- DTO and DAO concepts and MVC
- How to extract .war files in java? ZIP vs JAR
- What is the difference between run-time error and compiler error?
- What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- Test if element is present using Selenium WebDriver?
- Increase heap size in Java
- How to get javax.comm API?
- how to convert exe file to .jar file? [duplicate]
- How to print a table of information in Java
- set background color: Android
- Class Declarations for temperature program in Java
- Spring Boot – Cannot determine embedded database driver class for database type NONE
- Convert double to float in Java