I’m trying to bind some indexes and they’re corresponding strings so I tried using a HashMap like so:
public class NewClass {
static List<String> referenceList = new ArrayList<String>();
public static void main(String[] args) {
String x = "AGE_X";
String y = "AGE_Y";
//String w = "AGE_Z";
referenceList.add(x);
referenceList.add(y);
String text1 = "if ( AGE_X = = 10 ){\r\n"
+ " j = 1;"
+ " AGE_X = 5 ;"
+ " if ( true ) {"
+ " m = 4 / AGE_Y ;"
+ " }"
+ " }";
detectEquals(text1);
}
public static String detectEquals(String text) {
String a = null;
// text = TestSplitting.addDelimiters(text);
String[] newString = text.split(" ");
List<String> test = Arrays.asList(newString);
StringBuilder strBuilder = new StringBuilder();
HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>();
List<Integer> refList = new ArrayList<Integer>();
int index = 0;
for (int i = 0; i < test.size(); i++) {
a = test.get(i).trim();
//System.out.println("a=" + a);
strBuilder.append(a);
index = strBuilder.length() - a.length();
if (a.equals("if") || a.equals("=")) {
refList.add(index);
// System.out.println(refList);
signs.put(a, refList);
refList = new ArrayList<Integer>();
System.out.println(signs);
}
}
return a;
}
}
But I am not having any luck with it. I’m trying to get every “if” and “=” from that string(text1) and map them together but it seems I am doing something wrong, because my output looks like this :
{if=[0]}
{if=[0], ==[8]}
{if=[0], ==[9]}
{if=[0], ==[15]}
{if=[0], ==[23]}
{if=[26], ==[23]}
{if=[26], ==[36]}
I am looking for something like :
if=[0,26] and = =[8,9,15,23,23,36]
I need some help with figuring out where I’m making a mistake here? Or am I just not printing them out properly? Any help would be greatly appreciated.
Thank you,
Daniel
try this