Results 1 to 6 of 6

Thread: Spring begginer question

  1. #1
    Join Date
    Dec 2009
    Posts
    4

    Question Spring begginer question

    Hello I have just start learning spring and something is not clear to me at the beggining of the http://www.springbyexample.org/ book.
    Code:
    public void record(Candidate candidate){
    		int count = 0;
    		
    		if (!hVotes.containsKey(candidate)){
    			hVotes.put(candidate, count);
    		}
    		else{
    			count = hVotes.get(candidate);
    		}
    	}
    On this line "count = hVotes.get(candidate);" I get error. "get" method receive int and I try to pass candidate object, but there is no code in the book for candidate so I don't know what to do here? What am I doing wrong?
    Thanks

  2. #2
    Join Date
    Dec 2009
    Posts
    1

    Default

    your requirement is not pretty clear. can you write the whole code.

  3. #3
    Join Date
    Dec 2009
    Posts
    4

    Default

    your requirement is not pretty clear. can you write the whole code.
    From the book:
    Code:
    package org.springbyexample.vote;
    
    public class VotingBooth {
    
        VoteRecorder voteRecorder = new VoteRecorder();
        
        public void vote(Candidate candidate) {
            voteRecorder.record(candidate);
        }
        
        class VoteRecorder {
            Map hVotes = new HashMap();
            
            public void record(Candidate candidate) {
                int count = 0;
                
                if (!hVotes.containsKey(candidate)){
                    hVotes.put(candidate, count);
                } else {
                    count = hVotes.get(candidate);
                }
                
                count++; 
                
                hVotes.put(candidate, count);  
                
            }
        }
    
    }
    But there is no implementation for the candidate, so I created just simple type:
    Candite class
    Code:
    public class Candidate {
         public Candidate(){}
    }
    But when I try to do this "count = hVotes.get(candidate);" in code I get an error because hVotes.get(ask for int type not candidate). How to pass candidate here? I try with cast but Candidate object can not be casted to the int type.

  4. #4

    Default bug fix on java 6

    hi on java 6 this class
    PHP Code:
    import java.util.Map;
    import java.util.HashMap;

    public class 
    VotingBooth {

        
    VoteRecorder voteRecorder = new VoteRecorder();
        
        public 
    void vote(Candidate candidate) {
            
    voteRecorder.record(candidate);
        }
        
        class 
    VoteRecorder {
            
    Map hVotes = new HashMap();
            
            public 
    void record(Candidate candidate) {
                
    int count 0;
                
                if (!
    hVotes.containsKey(candidate)){
                    
    hVotes.put(candidatecount);
                } else {
                    
    count = ((Integer) hVotes.get(candidate)).intValue();
                }
                
                
    count++; 
                
                
    hVotes.put(candidatecount);  
                
            }
        }

    }

    class 
    Candidate {
      public 
    Candidate(){};

    compiles according to
    PHP Code:
    http://www.innovation.ch/java/java_compile.html 

  5. #5
    Join Date
    Dec 2009
    Posts
    4

    Default

    Thank you now it works

  6. #6

    Default or

    instead of using a non typed Map, using generics would have worked as well
    PHP Code:
    Map<Candidate,IntegerhVotes = new HashMap<Candidate,Integer>(); 
    and then the record method stays
    PHP Code:
    public void record(Candidate candidate) {
                
    int count 0;
                
                if (!
    hVotes.containsKey(candidate)){
                    
    hVotes.put(candidatecount);
                } else {
                    
    count hVotes.get(candidate);
                }
                
                
    count++; 
                
                
    hVotes.put(candidatecount);  
                
            } 

Posting Permissions

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