Jump to content
  • Announcements

    • spinynorman

      Mandriva Official Documentation

      Official documentation for extant versions of Mandriva can be found at doc.mandriva.com.   Documentation for the latest release may take some time to appear there. You can install all the manuals from the main repository if you have Mandriva installed - files are prefixed mandriva-doc.
    • paul

      Forum software upgrade   10/29/17

      So you may have noticed the forum software has upgraded !!!
      A few things that have changed. We no longer have community blogs (was never really used) We no longer have a portal page.
      We can discuss this, and decide whether it is needed (It costs money) See this thread: Here
willisoften

Java Keyboard input character problem

Recommended Posts

   public static char ReadChar()
  {
   char cha=' ';
   BufferedReader key = new BufferedReader(new InputStreamReader(System.in));
   try
   {
    cha = (char) key.read();
 	
   }
        catch(IOException e)
       { 
       }

    return (cha);
  }

 

I'm using this to read a character from the keyboard. Everything seems ok except that for some reasons £ € signs aren't being read. (windows xp) Can someone write a test harness and see what happens to them?

 

Incidentally if I cast the £ or € or ¬ character as an int I print out 13.

 

Other characters may also be missing

 

Anything using Alt Gr prints out 13 when cast as an int.

 

Is this a Java peculiarity or my error.

 

 

 

 

class Trial01
{

 public static void main(String [] args)
 {


   System.out.print("Character? "); 
   char c = KeybInput.ReadChar();

         System.out.print( c + "\n");

 
 
 }//ends main
}

Edited by willisoften

Share this post


Link to post
Share on other sites

First up - you don't need to put the return value in brackets - probably not a big deal...

 

Second - try actually catching any errors - you may get some more helpful information

 

catch IOException e){
            System.out.println("Problem: " + e);
         }  

 

Also - at this stage you don't really need to cast to a char. try just using an int for (no need to cast :P ) and see if it works

Share this post


Link to post
Share on other sites

I'm not a java dude, but is the key input routine you are using designed to return ascii or unicode? If ascii it will always return a value between 0-255. If unicode, it will return a two byte char. Do some googling on code tables for more info. Basically, you press a key and if it goes throug a code table, it gets converted to the character set you are using. So char 13 = this in one char set, and that in another char set. But if no char set is involved or the key routine isn't expected to use them then its ascii..

Share this post


Link to post
Share on other sites

the method returns an int - a simple integer number

 

I guess this means it ascii?

Edited by phunni

Share this post


Link to post
Share on other sites

Does key.read() return an int? If so then try checking the upper bits. I'm just guessing since I have no clue about Java.

 

Glitz.

Share this post


Link to post
Share on other sites

erm... look closely at the code on that page and the code above...

 

BTW - Java is a high level language. I've not done much personally with reading in characters from a keyboard as they are pressed, but it shouldn't invlove having to parse anything at a low level.

 

Anyway - the question is: where is willisoften? As we discuss his code he has wandered off... :D

 

Perhaps this discussion is less interesting that we thoguht it was!

Share this post


Link to post
Share on other sites

Sorry guys, I've been busy.

 

I didn't really get a chance to do anything about this as all sorts of things happened to me at once!

 

I'll investigate some more but the ascii / unicode thing looks likely - except that all the missing characters where turning up as the single value 13 when cast as int.

 

I'm revising for exams at the moment so dear knows when I'll get back to this, not as though it's life-threatening!

 

Cheers!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×