0

I was wondering if I could get some quick help on some java code. I created an array and each position/seat has a specific 'price'. I need to write a program that asks the user to either pick a seat or price. I've already completed the first half of finding a specific seat location picked by the users input and replacing it with 0 but I've been having trouble with the second half of taking the users desired seat price and changing it to 0. There are multiple seats at the price they choose so I just need to pick a random one and change it to 0. I'll cut out a bunch of code to make it easier to read but basically what I need help with is towards the bottom:

Scanner in = new Scanner(System.in);

    String seat = "";
    String price = "";

    int[][] seating = new int[][]
        {
          { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
          { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
          { 10, 10, 20, 20, 20, 20, 20, 10, 10, 10 },
          { 10, 10, 20, 20, 20, 20, 20, 10, 10, 10 },
          { 10, 10, 20, 20, 20, 20, 20, 10, 10, 10 },
          { 10, 10, 20, 20, 30, 30, 20, 20, 10, 10 },
          { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
          { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
          { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },

        };
    System.out.println("\nHere is a map of the current seating:\n");
    printArray(seating);

    System.out.println("\nWould you like to pick a seat or a price?\n");
    String decision = in.nextLine();

    if(decision.equals("price")) 
        {
            System.out.println("\nWould you like to pay 10, 20, 30, 40, or 50 dollars?\n");
            int pickedPrice = in.nextInt();
            //HELP HERE //replace a random seat with the selected price to 0 in the array

        }

I've only come here as a last resort as I've looked all over the place and couldn't find any help. Thanks in advance, I really appreciate it!

2
  • How random does it have to be? Couldn't you just traverse the array like you are doing now to print it and at the first instance of a 10, 20, 30, 40, or 50, you turn that one to 0 and update the global copy? At the next time, it just picks the next available 10, 20, 30, 40, or 50. Commented Jan 28, 2014 at 19:55
  • 1
    Basic plan: (1) Define a simple class Seat with two members, "row" and "seat number". (2) Declare an ArrayList<Seat> a and initialize it to empty. (3) Use a double loop to go through seating. Every time a price matches the user's price, add the "row" and "seat number" of the seat to the a. (4) Use a.size() to get the number of entries N in the ArrayList, and generate a random integer from 0 to N-1. (5) Use a.get to get the Seat for the random number. That will give you the row and seat number, and then I think you can do the rest. Commented Jan 28, 2014 at 19:55

1 Answer 1

1

You have a few options here. The two I would consider are below.

  • Randomly select an item and check if it meets your condition. If it doesn't, pick another one.
  • Make another list containing only the elements that meet your condition, and then pick one at random.

If you choose the first option, it would look something like this.

do {
    Random generator = new Random(); 
    int row = generator.nextInt(seating.length);
    int seat = generator.nextInt(seating.length);
} while(seating[row][seat] != pickedPrice);

seating[row][seat] = 0;

Of course, this approach turns out to be inefficient if all but one seat is taken. If you chose to tackle the second option, it would be more efficient, but the code would be more complicated.

ArrayList<Point> matches = new ArrayList<>();
for(int row = 0; row < seating.length; row++) {
    for(int seat = 0; seat < seating[0].length; seat++) {
        if(seat == pickedPrice)
            matches.add(new Point(row, seat));
    }
}

Random generator = new Random();
Point p = matches.get(generator.nextInt(matches.size()));
seating[p.x][p.y] = 0;
Sign up to request clarification or add additional context in comments.

1 Comment

@ajb Fixed. Good catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.