0

When I use printf it gives me "???"

import java.util.Scanner;

public class Homework {

    public static void main(String[] args) {


        Scanner kbScanner=new Scanner(System.in);
            double m,c , m2 , c2;
            System.out.println("Enter the coefficients of the first line: ");
            m= kbScanner.nextDouble();
            c= kbScanner.nextDouble();
            System.out.printf("The first line equation is: y = %.1f x + %.1f  \n" , m,c );



    }

}

OUTPUT: Enter the coefficients of the first line:

1 2

The first line equation is: y = ??? x + ???

WHY? How can I fix it?

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • 1
    are you running this on a special device? – saeed foroughi Feb 14 '20 at 19:46
  • Is my Laptop a special device? No. – habib mohammed Feb 14 '20 at 19:48
  • 2
    This should be working fine: https://ideone.com/bB6O7B – f1sh Feb 14 '20 at 19:48
  • 1
    your laptop is not special, but what is the result of `Locale.getDefault()`? or try `...printf(Locale.ROOT, "The first ....` - I suspect that the default locale is one that uses digits that are not available on your console (e.g. arabic digits) – user85421 Feb 14 '20 at 20:31
  • how? please ,write it as complete code then I will try it – habib mohammed Feb 14 '20 at 20:41
  • just add `Locale.ROOT` as an additional (first) argument to the `printf` method, that is `System.out.printf(Locale.ROOT, "The first line equation is: y = %.1f x + %.1f \n" , m,c );` - or add `Locale.setDefault(Locale.ROOT);` at the very start of the main method – user85421 Feb 14 '20 at 20:49
  • Locale cannot be resolved to a variable – habib mohammed Feb 14 '20 at 20:53
  • really, have you tried to `import` it ? eclipse should help you with that (or add `import java.util.Locale;` to the other import) – user85421 Feb 14 '20 at 20:54
  • the same way you did with `Scanner`! – user85421 Feb 14 '20 at 20:55
  • Yes!! Finally. it gives the exact value!! How can I fix the default locale as you call it? – habib mohammed Feb 14 '20 at 20:59
  • "as I call it"? well, it is what it is (and also how *Java* calls it)....since you are using Eclipse (as commented somewhere else) try setting your workspace, in Eclipse, to UTF-8 (Window - Preferences - General - Workspace - Text file encoding) – user85421 Feb 14 '20 at 20:59
  • Now it gives me Arabic numbers! I want English numbers. – habib mohammed Feb 14 '20 at 21:04
  • hey?! help me please – habib mohammed Feb 14 '20 at 21:09
  • maybe *use* a special device (set to a language that uses that digits); or set the so called - not only by me - "default locale"; or search the net like [how do I set the default locale for my JVM?](https://stackoverflow.com/q/8809098/85421); or even do not use `printf` – user85421 Feb 14 '20 at 21:14
  • my default text file encoding is Cp1256! How to add the Cp1252? – habib mohammed Feb 14 '20 at 21:30
  • you german guy, Thank You! may god bless you. The problem is coming from my device. Do I have to use those functions all the time? – habib mohammed Feb 14 '20 at 21:36
  • you must see what better suits you (eventually including some of the options from the [link](https://stackoverflow.com/q/8809098/85421) I posted in last comment): not use `printf`; use `Locale.ROOT` (or different one) in `printf` (where needed); `Locale.setDefault()` (for whole program); start java using `-Duser.language`; setting `JAVA_OPTIONS` to use `-Duser.language`; or even (try) changing the language of the system (not sure if that is acceptable/desirable, can mess up other applications) – user85421 Feb 14 '20 at 22:23

1 Answers1

0

What are m2 and c2 for? Just set them to .1*m and .1*c respectively in your code, then set your formatting specifiers to %f.

Your class will look something like this.

import java.util.Scanner;

public class Homework {

    public static void main(String[] args) {


        Scanner kbScanner=new Scanner(System.in);
            double m,c , m2 , c2;
            System.out.println("Enter the coefficients of the first line: ");
            m= kbScanner.nextDouble();
            c= kbScanner.nextDouble();
            m2= .1 * m;
            c2= .1 * c;
            System.out.printf("The first line equation is: y = %f x + %f" , m,c );

I think that should solve the issue. Good luck.

Nate T
  • 727
  • 5
  • 21