0

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

3
  • Expected output? Commented Jul 21, 2020 at 4:11
  • The expected output is
    – Bob Lander
    Commented Jul 21, 2020 at 4:15
  • I think you might need to rethink on the logic since I don't see how you can get the expected output with the given logic. Also my answer would only fix the error then. Commented Jul 21, 2020 at 4:26

1 Answer 1

0

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.

  • If you want the two integers to appear then it is System.out.printf("%d%d", fieldWidth, intValue);.
  • Else if it is string with integer it is System.out.printf("%s%d", "fieldWidth", intValue);.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.