Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a UTF-8 String as its Int eqivelents using a bufferedReader

Status
Not open for further replies.

timesign

Programmer
May 7, 2002
53
US
I am updating some code in a servlet that previously accepted a UTF-8 file and now gets its input from a textarea.


The problem: For any unicode character in the string the representation is '&#1492' while a comma for example will remain ',' . Therefore, when in.read() is called instead of returning the number '1492' it returns first '&' then '#' then '1' , 4 ,9 ,2 etc. each char at a singe time.

Any suggestions of methods that will handle the string differently so it returns a number?

Thanks a load.


Old Code------------------------
Code:
int ch;             //unicode char read from file

try{//try 1
   FileInputStream fis = new FileInputStream(userInputFile);
   InputStreamReader isr = new InputStreamReader(fis,"UTF8");
   BufferedReader in = new BufferedReader(isr);

   while ((ch = in.read()) > -1) {
This successfully gives me the integer of the UTF8 file which I then use for a comparison....
int would be equal to for example '1492'.


I replaced the code with ----------------------------------
Code:
int ch;             //unicode char read from file

try{//try 1
    StringReader isr = new StringReader(InputFile);
    BufferedReader in = new BufferedReader(isr);

    while ((ch = in.read()) > -1) {
        key = new Mapping();
	key.numChar = ch;
	location = Arrays.binarySearch(MapArray, key , new Comparator() {
-----
with input file being a String returned by the textarea.

 
Solved the problem,
Sharing it in case anyone else has / had / will have a similar problem.
Really simple!!
the accept-charset="UTF-8" tag must be placed not in the head tag but in the form tag!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top