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 / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
jason@cyberpine.com - 27 Dec 2005 15:37 GMT
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.
Chris Smith - 27 Dec 2005 16:07 GMT
> 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.

Second, you wrote:

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

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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



©2008 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.