-3

I have a string in the following format :

"(1,X) (3,2) (5,2) (7,2)"

Basically the first element can be any positive integer, the second element is either '1' 'X' or '2'. The string can be of varying length with the minimum being one element. I want to split this string and store each of the

(int, String) 

pairs in a HashMap with the int acting as the key and the String being the object. I'm not too sure how to use the split method here and would be grateful for a short guidance.

Alk
  • 5,215
  • 8
  • 47
  • 116
  • 1
    The Internet (as well as this site) is rife with examples of *exactly* what you want. Why not make a small effort to help yourself by searching before asking for help? – MarsAtomic May 31 '15 at 20:42
  • @MarsAtomic I did, but all the examples I found involved storing the result in an Array, not a HashMap – Alk May 31 '15 at 20:45
  • Furthermore, what's wrong with searching for how to move the contents of an array into a hashmap, which is what your real question seems to be? That very question has been asked on Stack Overflow already. I found it by *searching*. – MarsAtomic May 31 '15 at 20:47
  • Well I would appreciate a link or some help on how to do it, rather than a long discussion on why I don't find it – Alk May 31 '15 at 20:49
  • possible duplicate of [Store an array in HashMap](http://stackoverflow.com/questions/7602665/store-an-array-in-hashmap) – Adam Link May 31 '15 at 21:21

1 Answers1

1

What you seem to be looking is

for each data like "(number,[1 OR X OR 2])" in text
    map.put(number, value)

You can easily do it with Pattern/Matcher. So you need code like

Map<String,String> map = new HashMap<>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(data);
while(m.find()){
    map.put(m.group("number"), m.group("value"));
}

Your regex can look like [(](?<number>\d+),(?<value>[1X2])[)] where

  • [(] represents ( literal
  • (?<number>\d+) represents group named number storing one or more digits
  • (?<value>[1X2]) represents group named value storing 1 or X or 2
  • [)] represents ) literal
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Thanks a lot, only problem I'm having is "this named group synthax is not supported" in the regex. Am I supposed to declare the groups before or am I missing something? – Alk May 31 '15 at 20:59
  • Which version of Java are you using? Named groups ware added in Java 7. – Pshemo May 31 '15 at 21:01
  • Android Studio, version 1.7 – Alk May 31 '15 at 21:03
  • But if you still have problems you can use group indexes instead of their names (indexes start from 1): http://ideone.com/e5GM3f – Pshemo May 31 '15 at 21:10
  • This is my code and the errors I'm getting written on the bottom. http://ideone.com/YAKO44 – Alk May 31 '15 at 21:13
  • Errors written on the buttom simply means that you didn't place your code inside code block which should be placed inside class. Also you didn't import necessary classes or interfaces like Map. Something tells me that these errors are not related to errors in your application. – Pshemo May 31 '15 at 21:16
  • I did import, I just copied the short part of code relevant to the problem to that link, not the whole class which is way too long. I'm executing this whole thing within a for loop which iterates through another hashmap which stores these strings, does it have to be within a seperate class? – Alk May 31 '15 at 21:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79274/discussion-between-alk-and-pshemo). – Alk May 31 '15 at 21:21