Results 1 to 3 of 3

Thread: Hashmap trouble

  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default Hashmap trouble

    Whenever I try to put keys and values into my Hashmap, I get the compiler error "array required, but java.util.Hashmap<java.lang.String, java.lang.String> found"

    Java Code:
        public JPanel panel;
        public JTextField english;
        public JTextField foreign;
        public HashMap<String, String> answers;
        public String[] phrases;
        public Scanner scan;
        public StringTokenizer tokens;
    
    
        public void createAndShowGUI() {
            panel = new JPanel();
            english = new JTextField(40);
            foreign = new JTextField(40);
            panel.add(english);
            panel.add(foreign);
        }
    
        public void getPhrases(String theFile) {
            scan = new Scanner(theFile);
            int count = 0;
            while(scan.hasNext()) {
                count++;
            }
            phrases = new String[count];
        }
    
        public void getAnswers() {
            answers = new HashMap<String, String>();
            for(int i=0;i<phrases.length;i=i+2) {
                answers.put(answers[i], answers[i+1]);
            }
        }
    What am I missing here? I thought you just simply put(K k, V v) into a Hashmap.

  2. #2
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    your variable answers is a HashMap. You cannot do answers[i] or answers[i+1]. Did you mean to say phrases[i] and phrases[i+1] ?

  3. #3
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

Similar Threads

  1. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 08:08 PM
  2. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 02:29 PM
  3. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02: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
  •