Results 1 to 7 of 7
  1. #1
    ask4soteria is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Question HELP ...DONT UNDERSTAND Error

    PHP Code:
    //this is my hash table function
     public void hashTable(String s, int randX) {
    
    
    
            int key1 = (randX % 9);
            int c = (randX % 5) + 1;
            int i = 0;
            Integer check;
    
           
            check = (Integer) hashtable.get(key1);
                  if (isEmpty(check)) {
    
                hashtable.put(key1, s);
    
            }
     else{
                System.out.println("is it empty?????" + isEmpty(check));
     }
        }
    
    // this is my boolean check
        public static boolean isEmpty(Integer p) {
            boolean isnull = false;
            // System.out.println("ok here p is : " + p);
            //any variables you would need
            if (p == null) //{
            {
                System.out.println("it is true");
                isnull = true;
            }
    
            return isnull;
    
        }

    for some reason once the boolean fails it gives me this error





    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at HashFunc.hashTable(HashFunc.java:237)
    at HashFunc.actionPerformed(HashFunc.java:166)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6352)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:611 7)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4714)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:45 44)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:45 44)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 635)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I can't see what your hashtable variable is or what put() is supposed to do, but I can tell that put() is trying to accept 2 integer variables and is receiving an integer and a string ( hashtable.put(key1, s); ).

  3. #3
    ask4soteria is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    ok put() is accepting two variables. an int(key) and a string(item). the problem is on my boolean check " isEmpty()

    whenever i fails it never goes true it just spits the error out.

    // i used the inbuilt hash table function.
    Hashtable hashtable = new Hashtable(9);

    hashtable.put(key1, s);// this put is part of the lib.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    put() is trying to accept 2 integer variables and is receiving an integer and a string
    I think it's failing on
    Java Code:
    check = (Integer) hashtable.get(key1);
    with
    Java Code:
    ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    Evidently that's a Hashtable<Integer, String> and the reference returned from get(...) can't be cast to an Integer.

    db

  5. #5
    ask4soteria is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok so how do i fix it?

    Thank you

  6. #6
    ask4soteria is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    also to make that an object i have to type cast it as (Integer) if not its not gonna work..

    if have any other ideas pls lemme know thank you

  7. #7
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    The solution depends upon wat does hashtable.get(key1) return ? does it return a valid value parsable to Integer ???? .
    Why don't u use a Hashtable<Integer,Integer> instead.
    Last edited by Vinod Mukundan; 11-26-2010 at 11:33 AM.
    _______________________________________________
    give me beans .........

Similar Threads

  1. Replies: 3
    Last Post: 07-28-2010, 02:49 PM
  2. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 07:53 AM
  3. Dont understand Return Statement.
    By ocean in forum New To Java
    Replies: 6
    Last Post: 10-22-2009, 12:06 PM
  4. Data Files - A problem that I dont understand :D
    By Exhonour in forum New To Java
    Replies: 7
    Last Post: 01-20-2009, 05:13 AM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 10:03 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
  •