[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find the last index of a character, split it, and add it to an array

Posted on 2006-04-06
13
Medium Priority
?
462 Views
Last Modified: 2010-04-16
Hello Experts,

I want to split a string into an array in chunks of 10, but here's the catch:  I need to find the last index of a comma (,) in the chuck and split it into an array, even if it's less than 10.

For example:

String s = "1234567890,555,666666,9999,44,222222222";

// My first array should contain the value "1234567890". (10)
// My second array should contain the value "555,666666" (10)
// My third array should contain "9999,44" (7)
// My fourth array should contain "222222222" (9)

Here is the code I have so far but it's splitting the string into chunks of 10 regardless of the last array.

String chopSize = 10;
String toSplit = "1234567890,555,666666,9999,44,222222222";
List filler = new ArrayList();  
 for (int i=0, from=0, to=0, length = toSplit.length(); chopSize*i < length; i++) {
            from = chopSize*i;
            to = Math.min(from + chopSize, length);
            final String substring = toSplit.substring(from, to);
            filler.add(i,substring);
        }

Can someone tell me where I'm going wrong or point me in the right direction?  Thanks!!
0
Comment
Question by:jollykh
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16396621
why not use split:

Strin[] split = s.split(",");

0
 

Author Comment

by:jollykh
ID: 16396643
Because that splits the string at each "," interval so my results will be:

1234567890
555
666666
9999
44
222222222

... and that is not what I want.
0
 
LVL 92

Expert Comment

by:objects
ID: 16396652
then you loop thru, concatenating the tokens such that the length is <=10.
for each token
    if current lenght + next token length <= 10
        append token to current
    else
        add current to result
        clear current
    end if
loop
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16396779
           String s = "1234567890,555,666666,9999,44,222222222";
            Pattern p = Pattern.compile("([^,]{10}|[^,]+,[^,]+|[^,]+)");
            Matcher m = p.matcher(s);
            while (m.find()) {
                  System.out.println(m.group(1));
            }


prints

1234567890
555,666666
9999,44
222222222


but give it thorough testing as it may violate your rules in certain cases
0
 

Author Comment

by:jollykh
ID: 16396793
Thanks CEHJ!!!  That is exactly what I was looking for.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16396808
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16399634
>>with NO explanations in such question

v. difficult to explain regex
0
 
LVL 20

Expert Comment

by:Venabili
ID: 16399828
Not that difficult to give a URL for a tutorial for this :)And links on whta Matcher, Pattern and so on are :)
0
 

Author Comment

by:jollykh
ID: 16401192
CEHJ is right.....the Matcher and Pattern are fairly difficult to explain.

One thing I noticed, however, was that the code doesn't work like it should.  If you replace the string "1234567890,555,666666,9999,44,222222222" with something different like : "123,456,7777,8833,345345,345,3,4,5,6,6,6,7,8,345" it doesn't work.  The prgram doesn't take the first 10 characters and finds the last instance of a "," and prints it out.  In other words, the program is not very dynamic and it needs to be reworked.  I will try to figure out a more robust solution and show the solution once I am finished.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16402952
Well it's difficult to understand your rules fully. The first three tokens returned are

123,456
7777,8833
345345,345

which is actually what i though you wanted ...
0
 
LVL 92

Expert Comment

by:objects
ID: 16404580
> One thing I noticed, however, was that the code doesn't work like it should.

You can always reopen the q and I'll help you with implementing what I suggested :)
0
 

Author Comment

by:jollykh
ID: 16406912
Hi CEHJ,
Yes, you are correct.  That was what I wanted, but that was just an example of the requirement.  The example string I gave you, "1234567890,555,666666,9999,44,222222222", was just that: an example but the string can be changed to reflect other values.  
I plan on taking a closer look at the program this weekend as I really don't understand the Pattern and Matcher objects.  If I can't figure it out then I may have to reopen the question.
thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question