MandrivaUsers.org : Java Keyboard input character problem - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Java Keyboard input character problem

#1 User is offline   willisoften 

  • Awesome
  • Group: Members
  • Posts: 255
  • Joined: 25-September 02

Posted 21 November 2003 - 09:12 PM

   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
}

This post has been edited by willisoften: 22 November 2003 - 11:16 AM

0

#2 User is offline   phunni 

  • Friendly Englishman
  • Group: Members
  • Posts: 3,210
  • Joined: 13-November 02

Posted 04 December 2003 - 10:11 AM

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
Old School is the way forward Posted Image

Je suis le champignon! Posted Image

Posted Image

Posted Image
0

#3 User is offline   Cannonfodder 

  • Honorary Admin
  • Group: Members
  • Posts: 3,038
  • Joined: 07-September 02

Posted 04 December 2003 - 12:42 PM

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..
"Have you kissed your local Propeller Head today?"
The answers to Frequently Asked Questions
Posted Image
0

#4 User is offline   phunni 

  • Friendly Englishman
  • Group: Members
  • Posts: 3,210
  • Joined: 13-November 02

Posted 04 December 2003 - 12:52 PM

the method returns an int - a simple integer number

I guess this means it ascii?

This post has been edited by phunni: 04 December 2003 - 12:53 PM

Old School is the way forward Posted Image

Je suis le champignon! Posted Image

Posted Image

Posted Image
0

#5 User is offline   Cannonfodder 

  • Honorary Admin
  • Group: Members
  • Posts: 3,038
  • Joined: 07-September 02

Posted 04 December 2003 - 04:43 PM

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 :)/>
"Have you kissed your local Propeller Head today?"
The answers to Frequently Asked Questions
Posted Image
0

#6 User is offline   Glitz 

  • Awesome
  • Group: Members
  • Posts: 402
  • Joined: 19-September 02

Posted 04 December 2003 - 05:20 PM

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.
0

#7 User is offline   Cannonfodder 

  • Honorary Admin
  • Group: Members
  • Posts: 3,038
  • Joined: 07-September 02

Posted 04 December 2003 - 08:57 PM

Check this out... looks like its more up your alley..

http://java.sun.com/j2se/1.3/docs/api/java...reamReader.html
"Have you kissed your local Propeller Head today?"
The answers to Frequently Asked Questions
Posted Image
0

#8 User is offline   phunni 

  • Friendly Englishman
  • Group: Members
  • Posts: 3,210
  • Joined: 13-November 02

Posted 05 December 2003 - 09:18 AM

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!
Old School is the way forward Posted Image

Je suis le champignon! Posted Image

Posted Image

Posted Image
0

#9 User is offline   Cannonfodder 

  • Honorary Admin
  • Group: Members
  • Posts: 3,038
  • Joined: 07-September 02

Posted 05 December 2003 - 09:47 AM

Oops! Serves me right for _______________ ... (insert excuse of the day)

:)/>:):)/>:)
"Have you kissed your local Propeller Head today?"
The answers to Frequently Asked Questions
Posted Image
0

#10 User is offline   willisoften 

  • Awesome
  • Group: Members
  • Posts: 255
  • Joined: 25-September 02

Posted 03 January 2004 - 08:55 PM

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!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users