• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching hashmap for array String [] occurences?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, thanks very much in advance for any pointers!

I have a college assignment and it's confusing me slightly.
I have a hashmap which is basically a timetable for the 3 schools at my college, each school has it's own courses and these courses are prefixed with a specific 3 letter prefix.

For instance, in the School of Computing there is CSN,IMD etc. I have created 3 string arrays, one for each school, containing the various prefixes for each course.

Now I can't seem to find the correct method for searching my hashmap under the conditions of the array lists. The end result is I have to find the percentage of 9 o'clock starts for each school so first I have to find the total amount of events. (Hope this all makes sense! :/)

Here is what I have so far...
This is an error free run, hence why the foreach is commented out, but still only prints 0?
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use your string .contains(); like "ABC".contains(); inside the for. [=
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints 0 because the variable "soc" was never incremented during the loop.

And why would that be? Well, here's the if-statement which controls that:



So, you take the Event "e" and then you use its "module" variable which apparently returns a String, or at least something with a substring() method. Let's assume it's a String for now. You take the first three characters and compare them to what? You compare them to a String array. But a String can never equal a String array, regardless of what its contents might be. So that if condition is always false. Which means that "soc" is never incremented and hence your code prints 0.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! I see, so I can't use a String array at all?

I did start by writing loads of IF statements but thought I'd save code by using arrays.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can use a String array. It's just that if you want to see if your string is one of the elements of that string array, you can't just use the "equals" method to do that. You have to do a bit more programming.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm.... would I be on the right track with a string buffer?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the solution isn't that obscure. You want to see if String X is equal to one of the entries in String Array Y, right? Then just compare X to each of the entries in Y and go from there.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This works!! Thank you very much for nudging me in the right direction!

Is this what you had in mind out of curiousity?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic