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 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.