Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Reading input from console (greek chars)

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading input from console (greek chars)

    Hello There

    The following example code works fine.

     public static void main(String[] args) {
                   [B] char a='α';[/B]
                    System.out.println(a);
                    System.out.println((int)a);
                }


    Output
    α
    945


    This means, i think, that greek chars can be handled. 'α' is displayed.

    But the problem arises when i try to read greek chars from input


    public static void main(String[] args) {
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                String str = "";
                while (!str.equals("exit")) {
                    str = in.readLine();                   //input is greek 'α'
     
                   char[] charArr;
                    charArr=str.toCharArray();
     
                    for(int i=0;i<str.length();i++){
                        System.out.println(charArr[i]);    //outputs '?' not expected 'α'
                        System.out.print((int)charArr[i]);  //and this outputs '63' as for all other greek chars!
                    }
     
                }
                System.exit(0);
            } catch (IOException e) {
     
            }
        }

    Output

    ?
    63

    Any ideas why the greek char isn't stored correctly in the array in the first place?
    Last edited by lemmyz; August 15th, 2010 at 06:36 PM. Reason: highlighted code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading input from console (greek chars)

    Look at the constructors for the InputStreamReader. One of them takes a charset arg.
    Perhaps your default charset doesn't support greek chars

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading input from console (greek chars)

    I added "UTF8" and then "Cp1253" (for greek only) but still the same.


     BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "Cp1253" ));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading input from console (greek chars)

    I can't test without some greek letters.
    Can you make a test file and attach it to your post?

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading input from console (greek chars)

    thanks for your help but won't you have to type greek? By the way i checked the declaration of InputStreamReader.java
    and netbeans shows several "mistakes". How can that be? it's the JDK!!!


  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading input from console (greek chars)

    Sorry, I don't use your IDE and don't know what the red lines mean. Are there error messages anywhere?

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading input from console (greek chars)

    Yep:

    1. cannot find symbol
    symbol: class StreamDecoder
    location: class java.io.InputStreamReader


    2. variable sd might not have been initialized

    3. exception java.io.UnsupportedEncodingException is never thrown in body of corresponding try statement

    on top of the file says:
    package sun.nio.cs does not exist


    i'm starting to think that there's something there....

    i use jdk1.6.0_21

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading input from console (greek chars)

    I don't think you supposed to be able to compile Sun's source.

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading input from console (greek chars)

    Well i know that but all these errors? i do not intend to complile Sun's source. I was just checking if all my imports work...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading input from console (greek chars)

    I don't know if the code you are looking at generates what is in the rt.jar file.

  11. #11
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading input from console (greek chars)

    Well it appears to be a lot harder than i thought.. thanks anyway for your hints

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 06:47 PM
  4. Reading String Input?
    By Morevan in forum Java Theory & Questions
    Replies: 1
    Last Post: January 18th, 2010, 12:16 PM
  5. [SOLVED] help with console input strnig....
    By mdstrauss in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 17th, 2009, 03:59 AM