Jump to content

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
Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
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..

Link to comment
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!

Link to comment
Share on other sites

  • 5 weeks later...

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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...