This is how I call the method.
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
customPrint(7,10);
This is my method
public static void customPrint(int intValue, int fieldWidth) {
System.out.printf("%*%d", fieldWidth, intValue);
}
My motive is to use the fieldwith as a variable in the printf statement and output the integer with the field with. However I get an error: Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '*' Any solutions?
Expected output 1
Based on your question we cannot specify the correct output for your program. But the error is occurred due to the first argument given to the printf()
method.
System.out.printf("%d%d", fieldWidth, intValue);
. System.out.printf("%s%d", "fieldWidth", intValue);
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.