Q:

Words Counter using Dictionary

For homework I have to implement a Words Counter using a Dictionary.I have an inner class named Couple 
protected class Couple
    {   public Coppia(String key, Integer value) 
        {   setKey(key);
            setValue(value);
        }
        public String toString()
        {  return String.format("%-15s", key) + " | " + value; }
        public String getKey()
        {  return key; }
        public Integer getValue()
        {  return value; }
        public void setKey(String key)
        {  this.key = key; }
        public void setValue(Integer value)
        {  this.value = value; }
        //campi di esemplare
        private String key;     
        private Integer value;  
    }
}

I created the class WordsCounter with put,find,remove methods 
   class WordsCounter 
    {     private Couple [] a;
         private int inputSize;

         public WordCounter()
         { a=new Couple [10];
           inputSize=0;
         }

         public boolean isEmpty()
         {return inputSize==0;}

         public int size()
         {return inputSize;}

         public String toString()
         {String s="";
          for(int i=0;i<inputSize;i++)
           {s=s+a[i].toString()+"\n";
           }
           return s;
         }

         public void put(Comparable key, Comparable value)
         {if(inputSize==a.length)    //resize
            { Coppia [] newA=new Coppia [2*inputSize];
                for(int i=0;i<inputSize;i++)
                   { newA[i]=a[i];
                   }
                a=newA;
            }
           for(int i=0;i<inputSize;i++)       // if the word is already in, i replace the old value with the new one
             { if(a[i].getKey().equals((String)key))
                   { a[i].setValue((Integer)value);
                       return;
                    }
             }
  //otherwise i add the word in the array
            a[inputSize++]=new Couple((String)key,(Integer)value);
            mergeSort(a,inputSize);

          }

          public void remove(Comparable key)
          { int pos=binarySearch(a,0,inputSize-1,(String)key);
                if(pos<0)
                    { throw  new MapItemNotFoundException();
                    }
                else
                  { a[pos]=a[inputSize-1];
                    inputSize--;
                    mergeSort(a,inputSize);
                  }
          }

          public int binarySearch(Couple [] a,int start,int end,String k)
          { if(start>end)
                 return -1;
           int mid=(start+end)/2;
           if(a[mid].equals(k))
                { return mid;}
           else if(k.compareTo(a[mid].getKey())<0)
               { return binarySearch(a,start,mid-1,k);
               }
           else if( k.compareTo(a[mid].getKey())>0)
               { return binarySearch(a,mid+1,end,k);
               }

           else return -1;
         }

        public Comparable find(Comparable key)   
        { int pos=binarySearch(a,0,inputSize-1,(String)key);
                if(pos<0)   // the word isn't inside the array ,so i throw an exception
                    { throw  new MapItemNotFoundException();
                    }
                else
                  { return a[pos].getValue();
                  }

       }

         public void mergeSort(Couple [] a,int input)
          { if(input<2)
                 return;
            int mid=input/2;
            Coppia [] first=new Coppia [mid];
            Coppia [] second=new Coppia [input-mid];

            for(int i=0;i<first.length;i++)
             { first[i]=a[i];
             }
            for(int i=0;i<second.length;i++)
             { second[i]=a[i+first.length];
             }

             mergeSort(first,mid);
             mergeSort(second,input-mid);
             merges(a,first,second);
           }

           public void merges(Couple [] a,Couple []  b,Couple [] c)
           { int i=0;
             int k=0;
             int j=0;

             while(i<b.length&&j<c.length)
               { if(b[k].getKey().compareTo(c[j].getKey())<0)
                     { a[i++]=b[k++];
                     }
                 else
                    { a[i++]=c[j++];
                    }
               }

             while(i<b.length)
                    { a[i++]=b[k++];
                     }

             while(j<c.length)
                   { a[i++]=c[j++];
                    }
           }

The main class is
public class ContatoreParoleTester
{
    public static void main(String[] args)
    {     
          FileReader read=null;

          try{ read=new FileReader(in.nextLine());

             }
          catch(IOException e)
           {

             }

        Scanner c=new Scanner(read);
        WordCounter parole=new WordCounter();
        while(c.hasNextLine())
         { Scanner token=new Scanner(c.nextLine());
          token.useDelimiter("[^A-Za-z0-9]+");
            while(token.hasNext())
             { 
               String tok=token.next();
                tok=tok.toLowerCase();
             try{
                   Comparable n=  parole.find(tok);   // i find the word ,if it isn't inside (the method throw an exception message) i catch the exception and i insert the word in the array with value=1; else i insert it in the array with the new value
                   parole.put(tok,(Integer)n+1);

                }
                catch(MapItemNotFoundException e)
                  { parole.put(tok,1);}

             }
         }
      System.out.println(parole);

    }
}

When I print the counter the variable "value" doesn't seem to update, in fact i got something like this :

word1::1 
word2::1
word3::1
etc.

I think that the update method doesn't work well, but I don't know why!:(
Could anyone help me please?
Thanks

A:

Your binary search doesn't work... It only ever returns -1. I know this because it if ever did, this line in main: parole.put(tok,(Integer)n+1); would create a ClassCastException

