StringBuffer charAt ?? how .. any other way under J2ME??? THANK YOU!

J

jason

Self proclaimed java newbie here .. I've done some research but I might
be going in the wrong direction on this on. I'm trying to build a
simple J2ME text file search that returns every LINE that contains a
STRING in a resource file text file. Problem is, apparently regex and
split are apparently not available in J2ME .. so I'm trying to do this
with StringBuffer .. Maybe there is a better way to do this??



=== code block

private String readHelpText()
{
InputStream is = getClass().getResourceAsStream("help.txt");
String[] loadit = null;
int chr = 0;
try
{
StringBuffer sb = new StringBuffer();
// Read until the end of the stream
while ((chr = is.read()) != -1)
{
sb.append((char) chr);
// below does not work
// loadit[chr] = sb.charAt(chr);
// code I need might go here if I could get charAt to work?
//

System.out.println(chr); // this display a sequence of
numbers out of order???

}

return sb.toString();
}
catch (Exception e)
{
System.out.println("Unable to create stream ");
System.out.println(chr);
}
return null;
}
}
====

Thank you for any information or help.
 
C

Chris Smith

Self proclaimed java newbie here .. I've done some research but I might
be going in the wrong direction on this on. I'm trying to build a
simple J2ME text file search that returns every LINE that contains a
STRING in a resource file text file. Problem is, apparently regex and
split are apparently not available in J2ME .. so I'm trying to do this
with StringBuffer .. Maybe there is a better way to do this??

Depends on what you mean.

No, there is not an API to do what you want.
Yes, there are improvements that can be made to your code.

Some suggestions:

First of all, you are casting bytes of your file to chars (unicode code
points) without looking at any specific encoding. This is equivalent to
using the ISO 8859-1 encoding, but the way you're doing it is ugly and
will cause people to assume that your code is buggy. If you really mean
to assume ISO 8859-1, then do so explicitly with:

Reader r = new InputStreamReader(is, "ISO8859-1");

And then use r.read() and cast to char as you're doing now, but it no
longer looks like you're clueless about character encodings.

// below does not work
// loadit[chr] = sb.charAt(chr);
// code I need might go here if I could get charAt to work?
//

There are so many things wrong here, I don't know where to start.

First, loadit is an array of String. You have a char. You can't use a
char where you need a String. If you really want to do the conversion,
you can use String.valueOf(...) -- but in this case, I'm pretty sure
that's not what you want to do.

Second, chr is the unicode code point for a character somewhere in
help.txt. Are you sure you want to use it to index into an array or a
StringBuffer?

Third, you initialized loadit to null. If you dereference it like that,
it will just throw an exception. Unless you actually intend for loadit
to be null, I suggest you remove that initialization. Then, of course,
you'll eventually have to figure out how big you do expect it to be, and
create an array and assign the reference.

I'm stopping now. You're making it really hard for yourself using
JavaME when you clearly don't know Java at all. Get a good book on
normal (SE) Java, and sit down and learn the language first. Then start
looking into the ME stuff.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top