Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:48:23+00:00 2026-05-20T07:48:23+00:00

Hey guys, am trying to write to do type casting in java, but i

  • 0

Hey guys, am trying to write to do type casting in java, but i keep getting

run: 
[(E,1), (H,1), (F,2), (G,2), (I,5), ( ,7)]
(H,1)
class datacompression.tuple
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to datacompression.tuple
    at datacompression.Chap3.huffman(Chap3.java:79)
    at datacompression.Chap3.histogram(Chap3.java:37)
    at datacompression.Main.main(Main.java:18)
Java Result: 1

this the source code:

import java.util.ArrayList;

/**
 *
 * @author Cubearth
 */
public class Chap3 {

    public static void huffman(ArrayList<tuple> list){
        //creates an huffman tree
        ArrayList<Node> huff = new ArrayList<Node>();
        for(int i = 0; i<list.size(); i++){
            huff.add(i, new Node(list.get(i), 2));
            huff.get(i).setScore(list.get(i).getB());
        }

        System.out.println(huff.get(1).getData().toString());
        System.out.println(huff.get(1).getData().getClass());
        while(huff.size()>1){
            String msg = ((tuple)(huff.get(0).getData())).getA()+ ((tuple)(huff.get(1).getData())).getA();
            Node tmp = new Node(msg, 2);
            tmp.setChild(huff.get(0), 1);
            tmp.setChild(huff.get(1), 1);
            tmp.setScore((huff.get(0).getScore()+huff.get(1).getScore()));
            huff.set(0, tmp);
            huff.remove(1);
            sort2(huff);
        }
        System.out.println(huff);
        Tree tmp = new Tree(huff.get(0));
        tmp.print(2);
    }

    public static void sort2(ArrayList<Node> list){
            //sort an arrayList of node, uesing insertion sort
        int pos, min;
        for(pos = 0; pos<list.size()-1; pos++){
            min = pos;
            for(int i = pos+1; i<list.size(); i++){
                if(list.get(i).getScore()< list.get(pos).getScore())
                    min = i;
            }
            if(min!=pos){
                swap2(list, pos, min);
            }
        }
    }

    public static void swap2(ArrayList<Node> list, int a, int b){
            //swap two indexes of list
        Node bucket = new Node(list.get(a).getData(), list.get(a).getNoc());
        list.set(a, list.get(b));
        list.set(b, bucket);
    }
}

why my check for class in the huffman method comes up as a tuple(), but am unable to cast it as one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:48:24+00:00Added an answer on May 20, 2026 at 7:48 am

    well, initially huff.get(0).getData() might come as a ‘tuple’, but in this line:

    huff.set(0, tmp);

    you set it to be a String (as tmp is a String), so in the next iteration it will complain

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys, i'm getting an exception on the following inner exception: {Value cannot be
hey guys im trying to save a url link in a html form but
Hey guys, just another little problem here! Trying to write a quiz for a
Hey guys, My array is printing it's values on page when: document.write(unsorted[i]+<br/>); but not
Hey guys, I've been trying to build something simple in Flash 8, and I
Hey guys, I have a problem (again). This time I am trying to use
Hey guys im trying to get a simple button masher up, What i want
hey guys im trying to do a SELECT Statement from a local SQLDB so
Hey guys i am trying to automate my system at work for sending artwork
Hey guys I am trying to implement the twitter for posting the comments about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.