1

Using the printf(), the console prints '?' for number variables. It works fine when I try other variables

public class Main {

    public static void main(String[] args) {
        boolean myBoolean = false;
        char myChar ='$';       
        String myString = "Hello world";
        int myInt= 30;
        double myDouble = 123.456;
        System.out.printf("boolean = %b\n",myBoolean );
        System.out.printf("Char = %c\n",myChar );
        System.out.printf("String = %s\n",myString);
        System.out.printf("int = %d\n",myInt);
        System.out.printf("double = %f\n",myDouble );
    }

}

the console prints out this:

boolean = false

Char = $

String = Hello world

int = ??

double = ??????????

I searched the web and couldn't find the exact problem. The closest thing I found was Link I checked the Enable project specific settings. I also used the formatf, but the result was the same as printf. enter image description here

behzadfam
  • 11
  • 2
  • 1
    Does this answer your question? [String.format prints 'question mark' as output](https://stackoverflow.com/questions/30049590/string-format-prints-question-mark-as-output) – jhamon Jun 14 '23 at 10:08
  • 1
    cannot reproduce: https://ideone.com/MeWgUk - The error is likely your font/encoding or something that you use to display the numbers. – OH GOD SPIDERS Jun 14 '23 at 10:09
  • What about `System.out.println(String.format(Locale.US, "int = %d\n",myInt));`? btw that should be `%d%n` since what you have is platform-specific – g00se Jun 14 '23 at 10:54
  • Thank you g00se, it works. However, I still don't now why the printf() and formatf() behave like that. – behzadfam Jun 14 '23 at 12:25

1 Answers1

0

Have you checked your eclipse project text file encoding (Window->Preferences->General->Workspace then the Text File Encoding (at the bottom of the panel for me)?

Mine is set to Cp1252 (which I looked up and is Windows default so that makes sense, as I'm using Eclipse on Windows). I see UTF-8 in the options there which looks sensible since it's used in a lot of places. If yours looks different to those, it might be worth trying to change it and see if you still get the behaviour. I tried changing it to some other things but could not reproduce your error but then my eclipse seems not to save the change so it may still be that setting.

Dan
  • 45
  • 4