Announcement Announcement Module
Collapse
No announcement yet.
Spring begginer question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    your requirement is not pretty clear. can you write the whole code.

    Comment


    • #3
      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.

      Comment


      • #4
        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 

        Comment


        • #5
          Thank you now it works

          Comment


          • #6
            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);  
                        
                    } 

            Comment

            Working...
            X