1

I am trying to find the closest available seat.However ,I did try something but i could not find it ,so far after some formatting i came back to my star position.Any suggestions.!

public void nextFree(String seats) {
        int colA = 0;
        int rowA = Integer.parseInt(seats.substring(1, 2));
        int newColB = 0;
        int newRowB = 0;

        if (seats.equalsIgnoreCase("a")) {
            colA = 1;
        } else if (seats.equalsIgnoreCase("b")) {
            colA = 2;
        } else if (seats.equalsIgnoreCase("c")) {
            colA = 3;
        } else {
            colA = 4;
        }

        for (int i = 0; i < this.table.length; i++) {
            for (int k = 0; k < this.table.length; k++) {
                if (table[i][k] == "XX") {
                    newRowB = i;
                    newColB = k;
                }
            }
            System.out.print("The seat " + colA + rowA + " is not available! The next availale is " + newColB + newRowB);
        }

    }
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
vkaleri
  • 19
  • 5
  • We're going to need a little more context than what you have given, what does `table[i][k] == "XX"` signify? Is that a free seat or the end of your table? How do you know a seat is free in your table? What are you looking for? – Draken Apr 27 '16 at 08:52
  • In the begging all the array is full with"--" and if a seat is booked it changes to "XX".i have 4 rows and 9 columns, and every row is symbolized with a letter from A to D . What i am looking for is if the use inputs the seat C5 for example to find the closets one around it. – vkaleri Apr 27 '16 at 08:57
  • 1
    At the moment, you are setting your seat to be the same as the first one you find that has a "XX" in it, which means it's occupied. You should be looking for "--" and you need to do further work to see how close the seat is to the original taken one – Draken Apr 27 '16 at 09:00
  • How i can find the distance of the free seat from the taken? – vkaleri Apr 27 '16 at 09:04
  • 1
    Check out this answer here: http://stackoverflow.com/questions/19894294/distance-between-elements-in-2d-array Or here: http://stackoverflow.com/questions/8224470/calculating-manhattan-distance – Draken Apr 27 '16 at 09:17
  • Hmm it is helpful ,thanks but I think i can not implement the same code to mine programme. Can i send you the hole code to help me further more if of course you want and can? – vkaleri Apr 27 '16 at 09:43

3 Answers3

1

Your main problem is here:

for (int i = 0; i < this.table.length; i++) {
  for (int k = 0; k < this.table.length; k++)

See how the two loops both walk the same distance? That cannot be right surely. Try this:

for (int i = 0; i < table.length; i++) {
  for (int k = 0; k < table[i].length; k++)

This now loops down each row (table.length) and then across each column in that row (table[i].length).

Also table[i][k] == "XX" should probably be table[i][k].equals("XX").

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Thank you but with these loops will I be available to find the closest one? – vkaleri Apr 27 '16 at 08:59
  • @vkaleri - It certainly should be possible. Keep working on the problem until you get it right. Using a debugger might be insightful for you. – OldCurmudgeon Apr 27 '16 at 09:41
0
public void nextFree(String seats) {
    int colA = 0;
    int rowA = Integer.parseInt(seats.substring(1, 2));
    int newColB = 0;
    int newRowB = 0;
    int distance = -1;
    //Would recommend changing this to a switch statement, might lake it clearer
    if (seats.equalsIgnoreCase("a")) {
        colA = 1;
    } else if (seats.equalsIgnoreCase("b")) {
        colA = 2;
    } else if (seats.equalsIgnoreCase("c")) {
        colA = 3;
    } else {
        colA = 4;
    }

    for (int i = 0; i < table.length; i++) {
        for (int k = 0; k < table[i].length; k++){
            //Calculating current distance away from our chair
            //This assumes i is the column and k is the row, you may need to swap those values around
            int curDistance = Math.abs(i-colA) + Math.abs(k-rowA);
            //If the seat is free and the distance hasn't been set or the current distance is less than the previous distance
            if (table[i][k].equals("--") && (distance == -1 || distance > curDistance) {
                //Found a closer seat
                newRowB = k;
                newColB = i;
                distance = curDistance;
            }
        }
    }
    //Moved this out the for loop, it shouldn't have been inside
    System.out.print("The seat " + colA + rowA + " is not available! The next availale is " + newColB + newRowB);
}

Using the Manhattan distance as detailed here

This code does not assume you care about rows, if a seat is behind the current seat and is available, it could be considered closer than one in the same row

The Manhattan distance gives the following results when compared to the master seat:

            Row   Seat  Distance
Master       1     1       0
Next to      1     2       1
Behind       2     1       1
In front     0     1       1
Diagonal     2     2       2

Proof that it's working for me:

Taking seats

Results

Community
  • 1
  • 1
Draken
  • 3,134
  • 13
  • 34
  • 54
  • Thank you very much I can do it from here ! Just one more question with this am I able to find the closest if the closest is above? – vkaleri Apr 27 '16 at 10:02
  • It will search for the closest seat in a grid, so it could be to the side, above or below it. – Draken Apr 28 '16 at 06:58
  • Turns out I got my calculation wrong, just updating the code. It should be: `Math.abs(x1-x2)+Math.abs(y1-y2);` not Math.abs(x1-y1)+Math.abs(x2-y2); as I originally put it – Draken Apr 28 '16 at 07:05
  • Also don't forget, this equation assumes you don't mind the seat in front, behind or next to as being closest. If you want them in the same row, you'll need to add extra checks to your if function – Draken Apr 28 '16 at 09:40
  • It solved it but however want to check all the seats no matter where are they for example if i ts in every corner of the array,because if I put A1it will go out the array – vkaleri Apr 29 '16 at 16:54
  • How is it going outside the array, at what section are you looking at? – Draken May 01 '16 at 09:12
  • For example if I select the corners e.g A1 or A9 and D1 and D9 – vkaleri May 04 '16 at 10:09
  • The current code will not leave your current array, if you are using this bit: `for (int i = 0; i < table.length; i++) { for (int k = 0; k < table[i].length; k++){` Then you should stay inside your array, if you are leaving the array then something is wrong with the code that you have written I'm afraid – Draken May 04 '16 at 10:24
  • So this code can find every nearest free seat in my array? Everywhere? – vkaleri May 04 '16 at 12:56
  • Yep, as far as I'm aware – Draken May 04 '16 at 13:01
  • Alright maybe i did something wrong i will try to fix it! – vkaleri May 04 '16 at 13:18
  • There was a poblem see the outpou! – vkaleri May 05 '16 at 11:02
  • Enter number of seat: 2 Enter seat number : d4 Enter seat number : d5 You have booked 2 seats.Total Price is: 145.8The seat D4 is not available! The next availale is A1 – vkaleri May 05 '16 at 11:03
  • all the other seats are free and it should not outpout the msg and otherwise it is wrong again! – vkaleri May 05 '16 at 11:03
  • If it's still not working, you've introduced a bug somewhere, create a new question and someone should be able to help you. I've attached screenshots of the code working for me – Draken May 06 '16 at 06:20
  • Math.abs(x1-x2)+Math.abs(y1-y2); not Math.abs(x1-y1)+Math.abs(x2-y2); Here what i have to change in my code with my variables in order to work? – vkaleri May 06 '16 at 09:10
  • I updated the code with the correct maths equation: `//This assumes i is the column and k is the row, you may need to swap those values around int curDistance = Math.abs(i-colA) + Math.abs(k-rowA);` – Draken May 06 '16 at 09:26
  • I have uploaded in the following link 2 images of how your code works and it has some issues. [link](http://imgur.com/a/10eZc) – vkaleri May 06 '16 at 13:21
  • Is `i` the column or the row as you're doing two different things. You either need to have: `int curDistance = Math.abs(i-colA) + Math.abs(k-rowA);` and then `newRowB = k; newColB = i;` **OR** `int curDistance = Math.abs(k-colA) + Math.abs(i-rowA);` and then `newRowB = i; newColB = k;` At the moment, you're mixing both values up – Draken May 06 '16 at 13:32
  • 1
    It worked, thank you for your help. I really appreciate the time you have spent for me. – vkaleri May 06 '16 at 15:02
-1

You try to determine rowA from a two-character string.

Then you try to determine colA from a one-character string. You should probably rather use String.startsWith() here.

Hok
  • 847
  • 8
  • 16