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.

Dasman
  • 317
  • 4
  • 19
  • provide full error – Sunil Kanzar Jun 28 '17 at 04:46
  • 2
    Copy your output and past it on your question a cropped image helps no one to help you. – Jorge Campos Jun 28 '17 at 04:47
  • It has already answered [here](https://stackoverflow.com/questions/20686578/find-a-number-in-sorted-multidimentional-array-with-binary-search). – vanchann Jun 28 '17 at 04: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. – ajb Jun 28 '17 at 04:57
  • `Arrays.binarySearch` accepts only one-dimensional array. Convert seats into one-dimensional array https://stackoverflow.com/a/5466581/6743203 – Jay Smith Jun 28 '17 at 05:11

2 Answers2

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?

Henry
  • 42,982
  • 7
  • 68
  • 84
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.");
       }
Keshav
  • 821
  • 2
  • 12
  • 33