Results 1 to 3 of 3
- 10-05-2010, 07:57 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Combining strings and integers into collection
Hello,
I'm trying to correct a basic class that takes pairs of strings and their corresponding integers, and has a method to return the relevant integer when passed one of the strings. Currently, it has two arrays, and it has to keep track of the info relevant for each string and integer pair by an index number.
I would like to replace the two arrays with a single collection, but as I'm new to Java I'm having trouble. I think that a map would be a good idea, but when I tried using a HashMap, I ran into difficulty when I tried to return an integer, even when using generics.
Here is some selected code:
I'm not sure what to do about the error on the first return statement. It says that it requires an int, but find an Object. I thought that since I was using the Integer generic, it would be ok.Java Code:public class Marks { ... HashMap studentMarks = new HashMap <String, Integer> (); ... public int getMark(String name) { if (studentMarks.containsKey(name)) return (studentMarks.get (name)); \\ incompatible types error return 0; } ... }
Help or advice appreciated,
Thanks,
N
- 10-05-2010, 07:59 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
HashMap<String, Integer> studentMarks = new HashMap <String, Integer> ();
or better
Map<String, Integer> studentMarks = new HashMap <String, Integer> ();
- 10-05-2010, 10:23 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Combining these 2 projects
By fresh83 in forum New To JavaReplies: 14Last Post: 12-28-2009, 08:52 AM -
Finding Strings, booleans and Integers
By Pez in forum SWT / JFaceReplies: 1Last Post: 07-19-2009, 02:24 PM -
combining two separate codes into one
By jaiminparikh in forum Advanced JavaReplies: 22Last Post: 02-18-2009, 05:35 PM -
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM -
Help combining loops into 1 program.
By kewlgeye in forum New To JavaReplies: 5Last Post: 04-22-2008, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks