0

So I have this project for my college and I'm stuck here, I tried everything I had in mind to make this code save more than 1 slot, as I must save up to 100 into a matrix database. Everything works great but the program always overwrites the first line, never passes on to the second...Here's the code:

Reserve part method:

for (n=1; n<100; n++) {

    parkinglot[n][0] = Integer.toString(n);
    parkinglot[n][1] = JOptionPane.showInputDialog(null, "License plate: ").toUpperCase();
    String hourofreservation = JOptionPane.showInputDialog(null, "Reservation hour(hh:mm): ");
    parkinglot[n][2] = hourofreservation;
    parkinglot[n][3] = formatter.format(date);
    parkingtime = Integer.parseInt(JOptionPane.showInputDialog(null, "Hours : "));
    parkinglot[n][4] = Integer.toString(parkingtime);
    int totalfee = (toMinutes(parkingtime)/30) * fee;
    pay(totalfee);
    //SaveReservation(nrinmat, parkinglot);
    //save
    JOptionPane.showMessageDialog(null, "This is yout reservation" + "\n\n" + " | " + parkinglot[n][0] + " | " + parkinglot[n][1] + " | " + parkinglot[n][2] + " | " + parkinglot[n][3] + " | " + parkinglot[n][4] + " HOURS |");

    break;

    }   

Database method:

public static String[][] database(String [][]parkinglot)
        {
            System.out.println("This is database");
            for (int i = 1; i < parkinglot.length; i++) {
                for (int j = 0; j < parkinglot[i].length; j++) {
                    System.out.print(parkinglot[i][j] + "\t");
                }
                System.out.println();
            }

            return parkinglot;
        }
Iulian B.
  • 95
  • 1
  • 1
  • 8
  • You have break in your for-loop; It is causing the program to run for n=1 only. Also loop should start from n=0 – Rahul Agrawal Dec 23 '19 at 13:03
  • If I won't put that break, my program will continue running till the n=100. I have to stop after 1 run, and when I run the program again, it must continue from second line, third run, third line and so on, and it always overwrite the first line. `reserve` method must be one-time run and `database` must save every run from reserve. – Iulian B. Dec 23 '19 at 13:12
  • Seems fine from the code. If you want to not print out slots that haven't been reserved, then set them to a known null value (until reserved) and don't print those lines. – NomadMaker Dec 23 '19 at 13:48

1 Answers1

1

Your program is starting at 1 every time because you have this line:

for (n=1; n<100; n++)

which initializes n to 1 before you enter the loop. (As noted in a comment, usually you would initialize n to zero, but that's not your problem here.)

Later, you break out of the loop, when n is still 1. When you call this code again (I assume it's in a function), it reinitializes n to 1 at the start of the loop. So n is never anything other than 1.

If you only want to fill in one record each time you run the program, then you don't need a loop at all. You need to store the value of n somewhere (like on disk, or in a database) and then read it back when you run the program again. Or, if you're saving the contents of parkinglot somewhere and reading it back in, you could scan it (using a for loop) to find the first empty entry, and initialize n to that, something like:

int n = 1; // or 0
for (; n < parkinglot.length && parkinglot[n][0] != null; n++);
if (n < parkinglot.length) {
    populateParkingLotEntry(parkinglot, n);
} else {
    // No more slots left...
}
Willis Blackburn
  • 8,068
  • 19
  • 36
  • Which method I should use to store `n` on disk? – Iulian B. Dec 23 '19 at 14:36
  • Are you already storing the contents of `parkinglot` somewhere? You would have to be, in order for this conversation to make any sense. – Willis Blackburn Dec 23 '19 at 14:57
  • Just in the program memory, nowhere else – Iulian B. Dec 23 '19 at 15:23
  • 1
    You wrote "I have to stop after 1 run, and when I run the program again, it must continue from second line," so it sounds like you're letting the program exit and then running it again. If you're not, then you can just keep the current value of `n` in memory. Or, as I mentioned, you can just scan through `parkinglot` and look for the first empty slot. – Willis Blackburn Dec 23 '19 at 18:18
  • But populateParkingLotEntry gives me an error that says it must be a method, shoud I write `populateReserveEntry(parkinglot, n);` (That is how the method above database is called) – Iulian B. Dec 23 '19 at 23:04
  • I only have part of your program, so it's hard to understand how it all works. If you post a link to the complete program, I'll take a look at it. – Willis Blackburn Dec 24 '19 at 01:25