-1

I am trying to displaying Arabic words in in my program, but it shows strange symbols, and when I try to read Arabic inputs using Scanner the values are saving as question marks ?????????

Note: My IDE is VSCode

I spent 3 hours on searching but non of the previous answers worked with me here is the code.

import java.util.Scanner;

class arabicInputs{
    public static void main ( String [] args){

        String name1 = "بسم الله";
        System.out.print(name1);

        System.out.print("Enter your name: ");
        Scanner input = new Scanner(System.in, "UTF-8");

        name1 = input.nextLine() ;

        System.out.println(name1);
    }
}

here is the output enter image description here

1 Answers1

0

You require Arabic fonts in your OS.

As someone said be sure your prompt or terminal is set to UTF-8

You may need to install into your OS the Arabic "locale" so it can be set down in the taskbar for use with the prompt or terminal.

You should specify compiling your class file in UTF-8 for javac with the -encoding flag , and the source file should be saved in UTF-8.

You may need to consolidate string charset for it's charset version. String.format() method. Anything manipulating charsets requires wrapping in a java.io.UnsupportedEncodingException including any e.g. new String constructors nominating a charset! https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

try{
String name1 = String.format(new Locale("ar"),"%s",new String(name1.getBytes("UTF-8"),"UTF-8"));
catch(java.io.UnsupportedEncodingException charstex){
charstex.printStackTrace();
}

Visual Studio foreign languages https://code.visualstudio.com/docs/getstarted/locales Too, with windows, you can open the prompt as the top window, then right click on the little square with two letters in the taskbar along the bottom of the desktop, it should open a dialog widow with the list of languages for input and output. If Arabic is there it should be able to be selected. Putting in both a web charset for Arabic and a generic ordinary mainly used charset for Arabic can greatly help too after installing Arabic if it is not there.

Info on prompt chcp 65001 Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)

Java supported charset encodings cp1256 windows Arabic. ,. ISO8859-6. Latin/Arabic mixture. https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

Did youu see the "%s" argument in format() method? In Linux right clicking on the terminal edge should do a popup specific to the terminal, but if you only have an "en" Operating System you need to put in the international locales from the install disc, and in windows per OS for specific I think professional version is required or you may be able to download that locale, whether it's free or not I don't know.

Fonts You would need to install some generic Arabic font files too if the OS (Operating System) was only one language e.g. only English.

Samuel Marchant
  • 331
  • 2
  • 6
  • Can you tell me more about promoting the terminal to set to UTF-8? If you mean tiyping this command in the terminal 'chcp 65001', then I already did it and it's still not working Thanks in Advance – ABD ALRAHMAN ALASSAF Mar 14 '23 at 05:51