Hi,
I wrote the following Java Program. When I enter Arabic Course name, it displays non Arabic characters. Can you help me to read and write Arabic Character.
Thanks.

package p91;
import java.util.Scanner;
class GradeBook
{
    public void displayMessage(String courseName)
    {
        System.out.printf("Wellcome to the Grade Book Dr. Sherif!  for \n%s!\n", courseName);
    }
}
/**
 *
 * @author  
 */
class P91 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        
        
        GradeBook myGradeBook = new GradeBook();
        System.out.println("Please Enter the course name:  ");
        String nameOfCourse = input.nextLine();
        System.out.println();
        
            
        
        myGradeBook.displayMessage(nameOfCourse);
               
    }
}

Recommended Answers

All 8 Replies

I also tried using Arabic characters but the compiler didn't seem happy to accept them. I wonder if I only need to change something in the compiler's settings or do something else..

You have to change the encoding and use a stream reader. Look up internalisation and how to convert non-UTF back to UTF:

Oracle tutorials:

http://docs.oracle.com/javase/tutorial/i18n/index.html
http://docs.oracle.com/javase/tutorial/i18n/text/convertintro.html

Thank you for your answer. I had read the international code in the previous two links. These do not solve the problem. I submit my JAVA code, so please if any one can update it and return the solution? That is exactly what I need.
Thanks to all
Sherif

Thank you for your answer. I had read the international code in the previous two links. These do not solve the problem. I submit my JAVA code, so please if any one can update it and return the solution? That is exactly what I need.
Thanks to all
Sherif

post your current code so we can see how you tried to convert the characters

The Java code is up. See the first post. Thanks

AFAICT, if an explicit charset is not provided, Scanner class uses the default Charset when parsing the input stream. Two things you can try out:

  1. Add a SOP to the start of your program to print the default charset being used by adding the line: ` System.out.println(Charset.defaultCharset().name()) ` at the start of your program
  2. Change Scanner constructor to take in a explicit character set i.e. ` new Scanner(System.in, "UTF-8") `

If it still doesn't work, post a screenshot of how the session looks like (after hiding personal information of course).

Thank you for reply. But, it still does not work. I show you the new Java program and the output. Thank you in advance.

package arabicdisplay;
import java.nio.charset.Charset;
import java.util.Scanner;
class GradeBook
{
    public void displayMessage(String courseName)
    {
        System.out.printf("Wellcome to the Grade Book Dr. Sherif!  for \n%s!\n", courseName);
    }
}

class ArabicDisplay {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
        System.out.println(Charset.defaultCharset().name());
        Scanner input = new Scanner(System.in, "UTF-8");
        GradeBook myGradeBook = new GradeBook();
        System.out.println("Please Enter the course name:  ");
        String nameOfCourse = input.nextLine();
        System.out.println();
        myGradeBook.displayMessage(nameOfCourse);
    }
}

The output is as shown:

run:
UTF-8
Please Enter the course name:
قواعد البيانات

Wellcome to the Grade Book Dr. Sherif! for
����� ��������!
BUILD SUCCESSFUL (total time: 19 seconds)


see the Arabic letters "قواعد البيانات" are displayed as rubbish

There is one more change which I missed out in my previous post. Also, make sure that you use a UTF-8 aware output stream for writing out the arabic text. E.g.

final PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.printf("Welcome to the Grade Book Dr. Sherif!  for \n%s!\n", courseName);

Windows is bit quirky when it comes to encoding. These changes pretty much work on Ubuntu but I'm not sure if they would on Windows (which doesn't have an option for setting a global encoding).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.