Results 1 to 3 of 3
  1. #1
    neilvb is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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:

    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; 
    }
    ...
    }
    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.

    Help or advice appreciated,

    Thanks,

    N

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    HashMap<String, Integer> studentMarks = new HashMap <String, Integer> ();
    or better
    Map<String, Integer> studentMarks = new HashMap <String, Integer> ();

  3. #3
    neilvb is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Combining these 2 projects
    By fresh83 in forum New To Java
    Replies: 14
    Last Post: 12-28-2009, 09:52 AM
  2. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 03:24 PM
  3. combining two separate codes into one
    By jaiminparikh in forum Advanced Java
    Replies: 22
    Last Post: 02-18-2009, 06:35 PM
  4. Replies: 7
    Last Post: 04-28-2008, 05:28 AM
  5. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 10:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •