Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!