Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

newbie: j2me & isWhiteSpace

Thread view: 
Jeff - 18 Oct 2005 16:30 GMT
Hey

midp 2.0
J2ME Wireless Toolkit 2.2
JDK 5

When compiling the code below I get this error message:
cannot find symbol
symbol  : method isWhitespace(char)
location: class java.lang.Character
  if (Character.isWhitespace(e))

Here is my code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.IOException;
import java.io.*;
import java.lang.Character;
import java.util.*;

public String findspace(String data)
{
   boolean found = false;
   int pos = 0;
   String result = new String();
   char e = 'B';
   while (!found)
   {
       if (Character.isWhitespace(e))
      {
           found = true;
      }
      result += data.charAt(pos);
      pos++;
   }
   return result;
}

Please, give me suggestions to what I'm doing wrong (the compile error) here

Jeff
Oliver Wong - 18 Oct 2005 17:01 GMT
> Hey
>
[quoted text clipped - 7 lines]
> location: class java.lang.Character
>   if (Character.isWhitespace(e))

[snip]

   did you try isSpace() instead of isWhitespace() ? If the former works,
you may want to update your class library. Also, AFAIK the J2ME MIDP2.0
class library doesn't have the Character class.
Jeff - 18 Oct 2005 17:45 GMT
If  J2ME MIDP2.0 doesn't have the Character class, what do you suggest I use
instead?

>> Hey
>>
[quoted text clipped - 13 lines]
> you may want to update your class library. Also, AFAIK the J2ME MIDP2.0
> class library doesn't have the Character class.
Oliver Wong - 18 Oct 2005 21:44 GMT
> If  J2ME MIDP2.0 doesn't have the Character class, what do you suggest I
> use instead?

   You'll have to implement it yourself. (Ouch!)

   - Oliver
Darryl L. Pierce - 19 Oct 2005 14:16 GMT
>     You'll have to implement it yourself. (Ouch!)

Which you can't do since both licensing and implementations disallow
including java.lang.* classes in your code.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Oliver Wong - 19 Oct 2005 16:23 GMT
I'm going to combine some of Darryl L. Pierce's replies into this one big
message.

"Darryl L. Pierce" <mcpierce@gmail.com> wrote in message
news:ODr5f.37167$Lp.33159@bignews5.bellsouth.net...
>>     did you try isSpace() instead of isWhitespace() ? If the former
>> works, you may want to update your class library. Also, AFAIK the J2ME
[quoted text clipped - 3 lines]
> However it does *not* provide all APIs from JavaSE. The right thing would
> be to check the MIDP APIs, which excludes the isWhiteSpace() method...

   You are right about the MIDP having the Character class. I had
downloaded the MIDP API and looked at the "All Classes" list on the left
frame, noticed that "Character" did not appear there, and so assumed that
the Character class was not present in the library. However, if I click on
the package "java.lang", Character does show up, but not as a link (implying
that it's not documented, I guess).

>>     You'll have to implement it yourself. (Ouch!)
>
> Which you can't do since both licensing and implementations disallow
> including java.lang.* classes in your code.

   I'm not sure I understand what you're said here, but my understanding is
that the OP wants the functionality of the isWhitespace() method, and the
MIDP class library doesn't provide it, so I'm saying the OP should implement
that functionality himself, e.g.:

public class MyUtilityClass {
 public static boolean naiveImplementationOfIsWhitespace(char c) {
   return c = ' ';
 }
}

   I doubt that something like the above will put the OP in any legal
trouble.

   - Oliver
Roedy Green - 20 Oct 2005 01:51 GMT
>    I doubt that something like the above will put the OP in any legal
>trouble.

I think the legal trouble might come cannibalising the J2SE
Chararacter class and sticking it in MIDP.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Darryl L. Pierce - 20 Oct 2005 12:45 GMT
>>>    You'll have to implement it yourself. (Ouch!)
>>
>>Which you can't do since both licensing and implementations disallow
>>including java.lang.* classes in your code.
>
>     I'm not sure I understand what you're said here,

My statement is clear: no classes that are packaged as java[x].* are
allowed to be included in a MIDlet suite. This restriction exists both
as a licensing element (read the license for the WTK, the MIDP RI, etc.)
and as an implementation detail (if you include such a class then all
MIDP implementations will fail the suite during verification).

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Darryl L. Pierce - 19 Oct 2005 14:15 GMT
> If  J2ME MIDP2.0 doesn't have the Character class, what do you suggest I use
> instead?

Read the Javadocs rather than take incorrect advice at face value. MIDP
does provide java.lang.Character.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Darryl L. Pierce - 19 Oct 2005 14:15 GMT
>     did you try isSpace() instead of isWhitespace() ? If the former works,
> you may want to update your class library. Also, AFAIK the J2ME MIDP2.0
> class library doesn't have the Character class.

MIDP provides *all* of the java.lang wrapper classes, including
Character. However it does *not* provide all APIs from JavaSE. The right
thing would be to check the MIDP APIs, which excludes the isWhiteSpace()
method...

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Roedy Green - 18 Oct 2005 20:02 GMT
>When compiling the code below I get this error message:

By eye your code looks ok for desktop java.  You don't need to import
java.lang.* classes.

Perhaps JME does not support the Character class or all the methods in
it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 18 Oct 2005 20:03 GMT
> String result = new String();
the better way to write that is
String result = "";

you don't really want yet another copy of the null string kicking
around.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 18 Oct 2005 20:05 GMT
>public String findspace(String data)
>{
[quoted text clipped - 13 lines]
>    return result;
>}

Other than your compile problem, the code itself makes no sense.  e
never changes inside the loop so you can't ever terminate.

Normally you would do that with a for loop and a break. You can't
count on finding a whitespace before running off the end.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

David N. Welton - 18 Oct 2005 22:30 GMT
> Hey
>
[quoted text clipped - 7 lines]
> location: class java.lang.Character
>    if (Character.isWhitespace(e))

According to my O'Reilly book, it doesn't exist as part of J2ME.

ch != ' ' && ch != '    ' && ch != '\n'

is how I did it in Hecl.

Signature

David N. Welton
- http://www.dedasys.com/davidw/

Linux, Open Source Consulting
- http://www.dedasys.com/

Roedy Green - 18 Oct 2005 22:55 GMT
>ch != ' ' && ch != '    ' && ch != '\n'
>
>is how I did it in Hecl.
Another way to define whitespace is

boolean white = c <= ' ';

I presume you are not worried about high order chars.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Oliver Wong - 18 Oct 2005 23:07 GMT
>>ch != ' ' && ch != ' ' && ch != '\n'
>>
[quoted text clipped - 4 lines]
>
> I presume you are not worried about high order chars.

   For what it's worth, I'm often annoyed when I found out an otherwise
decent program fails to handle Unicode characters elegantly (lack of Unicode
support was one reason I stopped using WinAmp as my main mp3 player, for
example). The OP might want to take this into account when designing his/her
application.

   - Oliver
David N. Welton - 19 Oct 2005 07:42 GMT
>>>ch != ' ' && ch != ' ' && ch != '\n'
>>>
[quoted text clipped - 11 lines]
> example). The OP might want to take this into account when designing his/her
> application.

That's what j2me gives you, so you have to live with it, unless you seek
out all the various unicode characters...

As a more complete answer, here's what GNU Classpath does:

 public static boolean isWhitespace(char ch)
 {
   int attr = readChar(ch);
   return ((((1 << (attr & TYPE_MASK))
             & ((1 << SPACE_SEPARATOR)
                | (1 << LINE_SEPARATOR)
                | (1 << PARAGRAPH_SEPARATOR))) != 0)
           && (attr & NO_BREAK_MASK) == 0)
     || (ch <= '\u001F' && ((1 << ch)
                            & ((1 << '\t')
                               | (1 << '\n')
                               | (1 << '\u000B')
                               | (1 << '\u000C')
                               | (1 << '\r')
                               | (1 << '\u001C')
                               | (1 << '\u001D')
                               | (1 << '\u001E')
                               | (1 << '\u001F'))) != 0);
 }

Where readChar is defined thusly:

 /**
  * Grabs an attribute offset from the Unicode attribute database. The
lower
  * 5 bits are the character type, the next 2 bits are flags, and the top
  * 9 bits are the offset into the attribute tables.
  *
  * @param ch the character to look up
  * @return the character's attribute offset and type
  * @see #TYPE_MASK
  * @see #NO_BREAK_MASK
  * @see #MIRROR_MASK
  * @see CharData#DATA
  * @see CharData#SHIFT
  */
 // Package visible for use in String.
 static char readChar(char ch)
 {
   // Perform 16-bit addition to find the correct entry in data.
   return data[(char) (blocks[ch >> CharData.SHIFT] + ch)];
 }

So I guess if you wanted to do all that in your j2me app, that would
make things work.  Actually, you'd have to do more - you'd have to copy
those constants yourself, because they are not defined in the j2me
character class.

Ciao,
Signature

David N. Welton
- http://www.dedasys.com/davidw/

Linux, Open Source Consulting
- http://www.dedasys.com/

Roedy Green - 19 Oct 2005 09:53 GMT
>|| (ch <= '\u001F' && ((1 << ch)
>                             & ((1 << '\t')
[quoted text clipped - 6 lines]
>                                | (1 << '\u001E')
>                                | (1 << '\u001F'))) != 0);

If you wanted more speed you could use a java.util.BitSet to classify
char 0..1F.

then you would do just one shift.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

David N. Welton - 19 Oct 2005 13:28 GMT
>>|| (ch <= '\u001F' && ((1 << ch)
>>                            & ((1 << '\t')
[quoted text clipped - 11 lines]
>
> then you would do just one shift.

You might suggest that to the GNU Classpath folks, but for the j2me
situation mentioned in the subject, that's another blind alley because
java.util.Bitset does not exist in j2me, according to my O'Reilly book.

Ciao,
Signature

David N. Welton
- http://www.dedasys.com/davidw/

Linux, Open Source Consulting
- http://www.dedasys.com/

Roedy Green - 19 Oct 2005 14:47 GMT
>You might suggest that to the GNU Classpath folks, but for the j2me
>situation mentioned in the subject, that's another blind alley because
>java.util.Bitset does not exist in j2me, according to my O'Reilly book.

In that case you could do it like this:

/**
* test a J2ME method for determining whiteSpace
*/
public class WhiteTest
  {

  /**
   * 1 bit on at index of white characters
   */
  private static final long whites =
   ( 1L << 0 )
  | ( 1L << '\n' )
  | ( 1L << 0x0B )
  | ( 1L << 0x0C )
  | ( 1L << '\r' )
  | ( 1L << 0x1C )
  | ( 1L << 0x1D )
  | ( 1L << 0x1E )
  | ( 1L << 0x1F )
  | ( 1L << 0x20 );

  /**
    * is this character considered whiteSpace?
    * @param c char to test
    * @return true if this is a whiteSpace character
    */
  public static boolean isWhiteSpace ( char c )
  {
     if ( c > 0x20 ) return false;
     else return ( ( 1L << c ) & whites ) != 0;
  }

  /**
   * test harness
   *
   * @param args not used
   */
  public static void main ( String[] args )
     {
     System.out.println( Long.toBinaryString( whites ) );
     System.out.println( isWhiteSpace( ' ' ) );
     System.out.println( isWhiteSpace( '\r' ) );
     System.out.println( isWhiteSpace( (char) 0 ) );
     System.out.println( isWhiteSpace( '\u001F' ) );
     System.out.println( isWhiteSpace( 'A' ) );
     }
  }

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Thomas Hawtin - 19 Oct 2005 14:05 GMT
>>|| (ch <= '\u001F' && ((1 << ch)
>>                            & ((1 << '\t')
[quoted text clipped - 9 lines]
> If you wanted more speed you could use a java.util.BitSet to classify
> char 0..1F.

Speed as in the amphetamine?

> then you would do just one shift.

Take one class:

// Copyright will be LGPL, presumably - beware.
class GNU {
  public static boolean isWhitespace(char ch)
  {
    int attr = readChar(ch);
    return /*((((1 << (attr & TYPE_MASK))
              & ((1 << SPACE_SEPARATOR)
                 | (1 << LINE_SEPARATOR)
                 | (1 << PARAGRAPH_SEPARATOR))) != 0)
            && (attr & NO_BREAK_MASK) == 0)
      ||*/ (ch <= '\u001F' && ((1 << ch)
                             & ((1 << '\t')
                                | (1 << '\n')
                                | (1 << '\u000B')
                                | (1 << '\u000C')
                                | (1 << '\r')
                                | (1 << '\u001C')
                                | (1 << '\u001D')
                                | (1 << '\u001E')
                                | (1 << '\u001F'))) != 0);
  }
  static char readChar(char ch)
  {
     return ch;
  }
}

Compile and javap -c:

Compiled from "GNU.java"
class GNU extends java.lang.Object{
GNU();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static boolean isWhitespace(char);
  Code:
   0:   iload_0
   1:   invokestatic    #2; //Method readChar:(C)C
   4:   istore_1
   5:   iload_0
   6:   bipush  31
   8:   if_icmpgt       24
   11:  iconst_1
   12:  iload_0
   13:  ishl
   14:  ldc     #3; //int -268419584
   16:  iand
   17:  ifeq    24
   20:  iconst_1
   21:  goto    25
   24:  iconst_0
   25:  ireturn

static char readChar(char);
  Code:
   0:   iload_0
   1:   ireturn

}

Constant folding is one of the few optimisations javac does (it's
necessary for certain aspects of the spec).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 19 Oct 2005 14:50 GMT
>Constant folding is one of the few optimisations javac does (it's
>necessary for certain aspects of the spec).

Good point. However I think my code is easier to understand.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Darryl L. Pierce - 19 Oct 2005 14:14 GMT
> When compiling the code below I get this error message:
> cannot find symbol
> symbol  : method isWhitespace(char)
> location: class java.lang.Character

That method is not provided in the MIDP/CLDC.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Jeff - 19 Oct 2005 18:24 GMT
Thanks for all the suggestions

Jeff

>> When compiling the code below I get this error message:
>> cannot find symbol
>> symbol  : method isWhitespace(char)
>> location: class java.lang.Character
>
> That method is not provided in the MIDP/CLDC.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.