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

  • SEARCH
  • Home
  • 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 4012994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:18:59+00:00 2026-05-20T09:18:59+00:00

I have an array with types String, double, and float, and I need to

  • 0

I have an array with types String, double, and float, and I need to be able to search for a String in it. I tried to do a binary search, but I’m getting the following error when I run the program and attempt a search:

java.lang.ClassCastException: java.lang.String cannot be cast to Customer
    at Customer.compareTo(prog4.java:1)
    at java.util.Arrays.binarySearch0(Unknown Source)
    at java.util.Arrays.binarySearch(Unknown Source)
    at prog4.main(prog4.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

I haven’t found any other methods for searching a 3d array, so any help would be greatly appreciated.

Here is my code:

case 'b':
      System.out.println();
      System.out.println("Please enter a customer name:");

      String search = kb.nextLine(); //read the user's search
      int place; //location of result

      Arrays.sort(A);

      place = Arrays.binarySearch(A, search);

      if (place <= 0)
        System.out.println("Cannot find customer named " + search);
     else
     {
       System.out.println("Customer found:");  
       System.out.println(A[place]);
     }

            break;
  • 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-20T09:18:59+00:00Added an answer on May 20, 2026 at 9:18 am

    Rather than using an array, you would want to use a Map()

    Customer myCust = customers.get(search);
    

    Another option would be to create a new Customer

    Customer searchCust = new Customer(search);
    
    place = Arrays.binarySearch(A,searchCust);
    

    For the last section to find a customer correctly, you’ll need to implement the Comparable interface:

                                     // add this
    public class Customer implements Comparable<Customer> {
    
        // add this guy
        public int compareTo(Customer other) {
            return this.name.compareTo(other.name); // I'm assuming 'name' is the variable of the name
        }
    
    }
    

    or you can use the Comparator defined in @spinttheblack’s post.

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

Sidebar

Related Questions

Let's say I have an array I need to store string values as well
What is the best way to have an associative array with arbitrary value types
I have an array of a few million numbers. double* const data = new
I have a string(char*), and i need to find its underlying datatype such as
Is it possible to have an array that contains two different types of data?
I have the following possible strings that I need to turn into arrays so
i have a input tag which is non editable, but some times i need
I have an array of different type objects and I use a BinaryWriter to
I'm looking for a kind of array data-type that can easily have items added,
I have a vector-like class that contains an array of objects of type T

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.