willisoften Posted November 21, 2003 Report Share Posted November 21, 2003 (edited) 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 November 22, 2003 by willisoften Quote Link to comment Share on other sites More sharing options...
phunni Posted December 4, 2003 Report Share Posted December 4, 2003 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 Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted December 4, 2003 Report Share Posted December 4, 2003 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.. Quote Link to comment Share on other sites More sharing options...
phunni Posted December 4, 2003 Report Share Posted December 4, 2003 (edited) the method returns an int - a simple integer number I guess this means it ascii? Edited December 4, 2003 by phunni Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted December 4, 2003 Report Share Posted December 4, 2003 Probably, so if you are processing unicode characters you need to look into unicode routines. Time to google on unicode and java input la la de dah :) Quote Link to comment Share on other sites More sharing options...
Glitz Posted December 4, 2003 Report Share Posted December 4, 2003 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. Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted December 4, 2003 Report Share Posted December 4, 2003 Check this out... looks like its more up your alley.. http://java.sun.com/j2se/1.3/docs/api/java...reamReader.html Quote Link to comment Share on other sites More sharing options...
phunni Posted December 5, 2003 Report Share Posted December 5, 2003 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! Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted December 5, 2003 Report Share Posted December 5, 2003 Oops! Serves me right for _______________ ... (insert excuse of the day) :):):):) Quote Link to comment Share on other sites More sharing options...
willisoften Posted January 3, 2004 Author Report Share Posted January 3, 2004 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.