[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
Medium Priority
462 Views
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);
}

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

LVL 92

Expert Comment

ID: 16396621
why not use split:

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

0

Author Comment

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

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
clear current
end if
loop
0

LVL 86

Accepted Solution

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

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

LVL 86

Expert Comment

ID: 16396808
:-)
0

LVL 86

Expert Comment

ID: 16399634
>>with NO explanations in such question

v. difficult to explain regex
0

LVL 20

Expert Comment

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

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

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

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

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

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
Course of the Month20 days, left to enroll