• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is best way to create numeric index for a text value  RSS feed

 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to create a numeric value for a text field, so i can use the numeric value as an index in a regular array process, just to create some tallies of the occurances of that particular text field.

I started off thinking i could use an Arraylist, adding the text_value to the Arraylist and then getting the associated index for that text_value.

IE, somthing like this:
if araylist.contains("textfield") int i = araylist.get("textfield").

But you can duplicates in the Arraylist, that's not a viable method apparently!

So far, i've been trying various convoluded approaches and it's pretty ugly, but i'm sure that Java has an easier way right?

Here's what i've been playing with and i don't expect you to follow it, i'm just trying to hash out an approach so it's pretty unreadable and cut and pasted for this post:
<code>
if(hk.containsKey(dnam))
{
Integer hki=(Integer)hk.get(dnam);
}
else
{
hkicnt++;
Integer hki=new Integer(hkicnt);
hk.put(dnam,hki);
}
}
if (cm.containsKey(t.name.toLowerCase()))
{
if(chs.contains(t.name.toLowerCase())){} else
{
chs.add(t.name.toLowerCase());
if(t.name.toLowerCase().equals(dnam.toLowerCase())) {}
else
{
if(dlmcnt==0) {dlm="";dlmcnt++;} else {dlm="|";dlmcnt++;}
ia[hki]++;
System.out.println("hkincr:"+t.name+" "+ia[hki]);
cc=cc.concat(dlm+t.name.toLowerCase());
}
}
}

for (Iterator it=hk.entrySet().iterator();it.hasNext(); )
{
Map.Entry entry = (Map.Entry)it.next();
String key = (String)entry.getKey();
Integer hkiv = (Integer)entry.getValue();
int hki = hkiv.intValue();
System.out.println("hki:"+hki+" key:"+key+" tot:"+ia[hki]);
}

</code>
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i think i found it using indexOf, thanks anyway!

Have a nice weekend!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!