0

Objective

Trying to do a Binary Search for a value for an array of numbers. Then return the index and change it to 0.

Code so Far:

import java.util.*;

public class Theater {

    static int [][] seats = {
        {10,10,10,10},
        {20,20,20,20},
        {30,30,30,30},
        {40,40,40,40}
        };

public static void main(String [] args)
{

    //These two for loops print the whole array.
for ( int i=0; i<4;i++)
{
    for (int j=0; j<4; j++)
    {
        System.out.print(seats[i][j] + " ");
    }
    System.out.println("");
}


   // entering the value to be searched
    Scanner ticketReq = new Scanner(System.in);
    System.out.println("Enter the ticket price you want: ");
    int ticketVal = ticketReq.nextInt();

   int indexOfTicket = Arrays.binarySearch(seats, ticketVal);

   if (indexOfTicket > 0) {
       System.out.println("The index of element 12 is : " + indexOfTicket + ". It is purchased!");
   }
   else {
       System.out.println("Tickets with price " + ticketVal + " are no longer available.");
   }


}

}// Ends the static void block

Error I'm having: Error

Error Text:

Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Comparable
    at java.util.Arrays.binarySearch0(Unknown Source)
    at java.util.Arrays.binarySearch(Unknown Source)
    at Theater.main(Theater.java:31)

Any help is appreciated. Is it because I have an array of arrays? Any way to do a binary search for an array of arrays? I am really trying to make a theater app that has 4 scores of seats (10,20,30,40) and the user enters a monetary value and the programs picks any amount of seats based on that value.

5
  • provide full error Commented Jun 28, 2017 at 4:46
  • 2
    Copy your output and past it on your question a cropped image helps no one to help you. Commented Jun 28, 2017 at 4:47
  • It has already answered here. Commented Jun 28, 2017 at 4:54
  • It's not clear what a "binary search on an array of arrays" would look like. Do you want it to work the same as if you had a one-dimensional array where you just string all the numbers in all the rows together? If you did string them all together like that, would we be guaranteed that the numbers would be in order (a requirement for binary search)? If so, the best solution is to flatten the array yourself into a 1-dimensional array and do a binary search on that. You can Google "java flatten" for ideas on how to do that. Commented Jun 28, 2017 at 4:57
  • Arrays.binarySearch accepts only one-dimensional array. Convert seats into one-dimensional array stackoverflow.com/a/5466581/6743203 Commented Jun 28, 2017 at 5:11

2 Answers 2

0

In java Arrays.binarySerach(arr,element)works for single dimensional array.So you need to handle your case by converting your 2-D array into single dimensional array as shown below.

Note : It may not meet your requirement but you can think accordingly.

Scanner ticketReq = new Scanner(System.in);
        System.out.println("Enter the ticket price you want: ");
        int ticketVal = ticketReq.nextInt();
        int indexOfTicket =-1;
        for(int i=0;i<seats.length;i++){
            indexOfTicket = Arrays.binarySearch(seats[i], ticketVal);
        }

       if (indexOfTicket > 0) {
           System.out.println("The index of element 12 is : " + indexOfTicket + ". It is purchased!");
       }
       else {
           System.out.println("Tickets with price " + ticketVal + " are no longer available.");
       }
Sign up to request clarification or add additional context in comments.

Comments

0

In Java, a two dimensional array is really a one dimensional array of arrays. Your search therefore tries to find an integer in an array with elements of type array of integers, leading to a type error since an integer is not an array of integers.

It is also a bit unclear what you expected to happen. How would the position of one integer in a two dimensional array be represented by a single index?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.