LeJOS lacks (?) String.split()

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

LeJOS lacks (?) String.split()

Postby MaR1oC » Sun Feb 24, 2013 5:13 pm

Hello,

I've started a LeJOS PC Project in my computer (using Eclipse IDE LeJOS NXJ Plugin) but I am facing a problem:

There is no split() method in the String class (java.lang.String). I have double checked the API and I didn't find anything referring to String.split()

How can I implement this method in my LEJOS NXJ Runtime??

Thanks in advance! :D :D
Robotic Platforms currently being used:
1. LEGO Mindstorms NXT v.2.0
2. Arduino Uno
Projects currently under development:
http://code.google.com/p/lego-mindstorms-3d-printing-machine/

OS: Linux (Ubuntu, Arch, Sabayon) | IDE: Eclipse (+LeJOS Plug-in)
Java: Amateur User :D :D
User avatar
MaR1oC
New User
 
Posts: 19
Joined: Thu Jul 05, 2012 8:14 am
Location: Athens, Greece

Re: LeJOS lacks (?) String.split()

Postby skoehler » Sun Feb 24, 2013 5:53 pm

Well, you do know that String.split() uses regular expressions? So in order to implement String.split(), you first need to implement regular expressions.
Well, and using String.split() is really not the best thing to do, performance wise. Even if it would exist on the NXT.

Be a programmer, and split the string yourself. It's really not that hard. I guess, you're just going to split by comma or spaces or whatever anyways.
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby MaR1oC » Sun Feb 24, 2013 8:24 pm

I am develioping a GCode Controller for the NXT Brick
Robotic Platforms currently being used:
1. LEGO Mindstorms NXT v.2.0
2. Arduino Uno
Projects currently under development:
http://code.google.com/p/lego-mindstorms-3d-printing-machine/

OS: Linux (Ubuntu, Arch, Sabayon) | IDE: Eclipse (+LeJOS Plug-in)
Java: Amateur User :D :D
User avatar
MaR1oC
New User
 
Posts: 19
Joined: Thu Jul 05, 2012 8:14 am
Location: Athens, Greece

Re: LeJOS lacks (?) String.split()

Postby skoehler » Sun Feb 24, 2013 8:25 pm

MaR1oC wrote:I am develioping a GCode Controller for the NXT Brick

And what would that be? And what should it tell me about why you want to use String.split? Do you actually need support for regular expressions?
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby gloomyandy » Sun Feb 24, 2013 9:07 pm

GCode is a CNC control language.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3003
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: LeJOS lacks (?) String.split()

Postby skoehler » Sun Feb 24, 2013 9:47 pm

gloomyandy wrote:GCode is a CNC control language.

Googled a bit, found some GCode sample code. Couldn't think of any task that couldn't be easily done with indexOf()+substring() instead of split().
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby MaR1oC » Mon Feb 25, 2013 2:50 pm

skoehler wrote:
gloomyandy wrote:GCode is a CNC control language.

Googled a bit, found some GCode sample code. Couldn't think of any task that couldn't be easily done with indexOf()+substring() instead of split().



I agree with you. It would take a long time :/

@skoelher Check out my project :) http://code.google.com/p/lego-mindstorms-3d-printing-machine/

UPDATE: I have been thinking of a solution like this:

Code: Select all
   private static String[] split(String s, String S) {
      String[] str = {};
      try {
         for (int i = 0; i<=S.length(); i++) {
            int c = S.indexOf(s) - 1;
            str[i] = S.substring(0,c);
            S = S.substring(c+1);
         }
         return str;
      } catch (Exception e) {
         return null;
      }
   }
Robotic Platforms currently being used:
1. LEGO Mindstorms NXT v.2.0
2. Arduino Uno
Projects currently under development:
http://code.google.com/p/lego-mindstorms-3d-printing-machine/

OS: Linux (Ubuntu, Arch, Sabayon) | IDE: Eclipse (+LeJOS Plug-in)
Java: Amateur User :D :D
User avatar
MaR1oC
New User
 
Posts: 19
Joined: Thu Jul 05, 2012 8:14 am
Location: Athens, Greece

Re: LeJOS lacks (?) String.split()

Postby skoehler » Tue Feb 26, 2013 1:28 am

MaR1oC wrote:@skoelher Check out my project :) http://code.google.com/p/lego-mindstorms-3d-printing-machine/


Yeah, I found your parser. Well, I don't know where to start. When I see that you compare two String object with ==, and when I see that you do these comparisons for Strings of one letter only - well, I just have to tell you that you think about using char more often. And furthermore, you probably want to learn about why comparing Strings with == does sometimes work - but not always - and why there is an equals() method to use instead.

Also, you're using SVN the wrong way. I see all the Eclipse project files in the root of the SVN, and trunk, branches, and tags seems to be subfolders of your project. You should create a subfolder for your project inside trunk. Move everything to that subfolder.
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby skoehler » Tue Feb 26, 2013 1:35 am

MaR1oC wrote:
Code: Select all
   private static String[] split(String s, String S) {
      String[] str = {};
      try {
         for (int i = 0; i<=S.length(); i++) {
            int c = S.indexOf(s) - 1;
            str[i] = S.substring(0,c);
            S = S.substring(c+1);
         }
         return str;
      } catch (Exception e) {
         return null;
      }
   }

The line S = S.substring(c+1); kills the performance. Well, at least for large strings which contains very the substring s very often. Also, arrays in Java do not grow dynamically. Use this:

Code: Select all
   private static String[] split(String s, String S) {
      ArrayList<String> list = new ArrayList<String>();
      int start = 0;
      while (start < S.length()) {
         int end = S.indexOf(s);
         if (end < 0)
            break;
         
         list.add(S.substring(start, end));
         start = end + s.length();
      }
      if (start < S.length())
         list.add(S.substring(start));
      return list.toArray(new String[list.size()]);
   }


The code comes without warrenty. I hope, I have catched all corner cases.
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby MaR1oC » Tue Feb 26, 2013 3:54 pm

skoehler wrote:
MaR1oC wrote:
Code: Select all
   private static String[] split(String s, String S) {
      String[] str = {};
      try {
         for (int i = 0; i<=S.length(); i++) {
            int c = S.indexOf(s) - 1;
            str[i] = S.substring(0,c);
            S = S.substring(c+1);
         }
         return str;
      } catch (Exception e) {
         return null;
      }
   }

The line S = S.substring(c+1); kills the performance. Well, at least for large strings which contains very the substring s very often. Also, arrays in Java do not grow dynamically. Use this:

Code: Select all
   private static String[] split(String s, String S) {
      ArrayList<String> list = new ArrayList<String>();
      int start = 0;
      while (start < S.length()) {
         int end = S.indexOf(s);
         if (end < 0)
            break;
         
         list.add(S.substring(start, end));
         start = end + s.length();
      }
      if (start < S.length())
         list.add(S.substring(start));
      return list.toArray(new String[list.size()]);
   }


The code comes without warrenty. I hope, I have catched all corner cases.


Your method returns that "the s String is out of range: -1 ".

Moreover, I really appreciate your help (all of you guys & perhaps girls) and it seems that it is a good way to learn by my faults :)
Robotic Platforms currently being used:
1. LEGO Mindstorms NXT v.2.0
2. Arduino Uno
Projects currently under development:
http://code.google.com/p/lego-mindstorms-3d-printing-machine/

OS: Linux (Ubuntu, Arch, Sabayon) | IDE: Eclipse (+LeJOS Plug-in)
Java: Amateur User :D :D
User avatar
MaR1oC
New User
 
Posts: 19
Joined: Thu Jul 05, 2012 8:14 am
Location: Athens, Greece

Re: LeJOS lacks (?) String.split()

Postby skoehler » Tue Feb 26, 2013 4:58 pm

How about this one?

Code: Select all
       private static String[] split(String s, String S) {
          ArrayList<String> list = new ArrayList<String>();
          int start = 0;
          while (start < S.length()) {
             int end = S.indexOf(s, start);
             if (end < 0)
                break;
             
             list.add(S.substring(start, end));
             start = end + s.length();
          }
          if (start < S.length())
             list.add(S.substring(start));
          return list.toArray(new String[list.size()]);
       }
skoehler
leJOS Team Member
 
Posts: 1107
Joined: Thu Oct 30, 2008 4:54 pm

Re: LeJOS lacks (?) String.split()

Postby MaR1oC » Wed Feb 27, 2013 12:24 pm

skoehler wrote:How about this one?

Code: Select all
       private static String[] split(String s, String S) {
          ArrayList<String> list = new ArrayList<String>();
          int start = 0;
          while (start < S.length()) {
             int end = S.indexOf(s, start);
             if (end < 0)
                break;
             
             list.add(S.substring(start, end));
             start = end + s.length();
          }
          if (start < S.length())
             list.add(S.substring(start));
          return list.toArray(new String[list.size()]);
       }


Worked well for me ;) ! Thanks!
Robotic Platforms currently being used:
1. LEGO Mindstorms NXT v.2.0
2. Arduino Uno
Projects currently under development:
http://code.google.com/p/lego-mindstorms-3d-printing-machine/

OS: Linux (Ubuntu, Arch, Sabayon) | IDE: Eclipse (+LeJOS Plug-in)
Java: Amateur User :D :D
User avatar
MaR1oC
New User
 
Posts: 19
Joined: Thu Jul 05, 2012 8:14 am
Location: Athens, Greece


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff