newbie: j2me & isWhiteSpace

J

Jeff

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
 
O

Oliver Wong

Jeff said:
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))

[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.
 
J

Jeff

If J2ME MIDP2.0 doesn't have the Character class, what do you suggest I use
instead?



Oliver Wong said:
Jeff said:
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))

[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.
 
R

Roedy Green

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

Roedy Green

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

Roedy Green

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;
}

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

Oliver Wong

Jeff said:
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
 
D

David N. Welton

Jeff said:
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))

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.

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

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

Roedy Green

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

Oliver Wong

Roedy Green said:
Another way to define whitespace is

boolean white = c <= ' ';

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
 
D

David N. Welton

Oliver said:
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.

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,
--
David N. Welton
- http://www.dedasys.com/davidw/

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

Roedy Green

|| (ch <= '\u001F' && ((1 << ch)
& ((1 << '\t')
| (1 << '\n')
| (1 << '\u000B')
| (1 << '\u000C')
| (1 << '\r')
| (1 << '\u001C')
| (1 << '\u001D')
| (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.
 
D

David N. Welton

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

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,
--
David N. Welton
- http://www.dedasys.com/davidw/

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

Thomas Hawtin

Roedy said:
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
 
D

Darryl L. Pierce

Jeff said:
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.
 
D

Darryl L. Pierce

Oliver said:
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...
 
D

Darryl L. Pierce

Jeff said:
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.
 
D

Darryl L. Pierce

Oliver said:
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.
 
R

Roedy Green

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' ) );
}
}
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top