简体   繁体   中英

How to read greek input from keyboard using the Scanner class?

Im studying java for some time now and i have just encountered this problem. When i try to read Greek letters with the scanner class,just everything goes wrong.

Example --in the main method

Scanner sc=new Scanner(System.in);
System.out.println(sc.nextLine());

--in console i type for example

->Ελλάδα

i get as a result:

Im using netbeans IDE.

Am i doing something wrong?Scanner support greek letters?Any alternative solution to this? Thanks.

**Just found the solution to my problem.As it seems the default encoding of netbeans wasnt utf-8 so i edited the netbeans installation/etc/netbeans.conf file. In the line starting with:netbeans_default_options i added to the end of the string this:-J-Dfile.encoding=UTF-8 so now it looks like this


netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.dpiaware=true -J-Dsun.zip.disableMemoryMapping=true -J-Dfile.encoding=UTF-8"


You need to set the Scanner default encoding to UTF8. This will allow you to display Greek characters correctly:

import java.util.Scanner;

public class Answer {
    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in, "utf-8");
        System.out.println("enter greek letters");
        String myStr = keyboard.next();
        System.out.println(myStr);
    }
}

Another thing to try is to make sure that NetBeans has the correct (UTF-8) encoding set "for the NetBeans application". Have a look at: https://www.muehlencord.de/wordpress/2015/10/16/netbeans-utf8-encoding/

This tells you how to Set "UTF-8 encoding on Netbeans".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM