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

    Default Combining strings and integers into collection


    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,



  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Rep Power


    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
    Rep Power

Similar Threads

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