0

I have currently a program that passes a collection of values from an hashmap to another class which stores them as strings. The info stored in the strings is as such:

Station 1=[1, Task 1, 10, false, 0, java.awt.Color[r=255,g=0,b=0]] Station 2=[2, Task 2, 10, false, 0, java.awt.Color[r=0,g=0,b=255]] Station 3=[3, Task 3, 10, false, 0, java.awt.Color[r=0,g=255,b=0]] Station 4=[4, Task 4, 10, false, 0, java.awt.Color[r=255,g=200,b=0]] Station 5=[5, Task 5, 10, false, 0, java.awt.Color[r=0,g=0,b=0]] 

And I am trying to split them into (f.e.):

Station 1 1 Task 1 10 false 0 java.awt.Color[r=255,g=0,b=0] 

I've first tried splitting each string but as there are "=" and "," on the last part of the string where the color value is set it gets all messed up.

I've also tried StringTokenizer() but I get problems on the last part aswell.

Is there an easier or effective way to split these strings as needed? Any suggestions are welcome!

Thanks in advance for your time and let me know if code is needed!

EDIT:

I have fixed my particular problem by passing the arguments correctly from the hashmaps.

public MultiMap stationmap = new MultiHashMap( ); public Collection getkeyValues(String s) { Collection values = (Collection) stationmap.get(s); return values; } 

This way, I can pass this collection to the class where I need them, and I can further iterate over it to get the values for the keys.

I'll leave this open as further answers might help people with similar string problems!

5
  • 1
    Sounds like a regex would be a better match here than a simple split, especially if the strings always fit a unique pattern. However, if you can change the way it's stored, using something more inherently serializable (e.g. XML or JSON) would make your life easier. Commented Aug 9, 2012 at 3:12
  • thank you for your answer! I have fixed my problem with the hashmaps methods but will edit my question to explain. Commented Aug 9, 2012 at 3:19
  • 1
    It is usually better to leave the question intact and simply answer your own question instead, rather than put your solution into the question. See meta.stackexchange.com/questions/17845/… Commented Aug 9, 2012 at 3:25
  • I understand, but the way I fixed my problem doesn't really match as an answer for similar problems (handling the strings) as this was just a problem caused by a mis-use of the hashmaps in the first place. I hope I am making myself clear, and thank you for your replies. Commented Aug 9, 2012 at 3:27
  • Well, yes and no. I actually think "don't use strings and pass the collection itself instead" is a very good answer to your question. Your first sentence (which I missed the first time around) states these are both in the same program. But I also see your point that it does not address the string problem directly... Commented Aug 9, 2012 at 3:35

3 Answers 3

1

If you're controlling the formatting of the string, you can format them into json and use the parsing there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I have fixed my problem with the maps methods, but will leave my question open and edit it to explain how I fixed it.
1

I'm not sure why you're working in this manner :) Wouldn't it be easier for the other class to accept a HashMap and handle it in the same way? If you're outputting it to a file, you could serialize it instead; hashmaps are serializable.

Comments

1

Using the google's library guava:

public static List<String> convertToList(String str) { List<String> list = Lists.newArrayList(Splitter.on(CharMatcher.anyOf("=[,")).omitEmptyStrings().trimResults().limit(7).split(str)); /** Returns: Station 1 1 Task 1 10 false 0 java.awt.Color[r=255,g=0,b=0]] */ int lastIndex = list.size() - 1; String last = list.get(lastIndex); String amendedValue = last.substring(0, last.length()-1); list.set(lastIndex, amendedValue); // Convert the last element "java.awt.Color[r=255,g=0,b=0]]" to "java.awt.Color[r=255,g=0,b=0]" return list; } 

Or:

public static List<String> convertToList2(String str) { str = str.substring(0, str.length()-1); return Lists.newArrayList(Splitter.on(CharMatcher.anyOf("=[,")).omitEmptyStrings().trimResults().limit(7).split(str)); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.