-1

This is my class:

public class autoTrainerID {
    private static int trainerID=100000;
    private int nexttrainerID;

    public autoTrainerID() {
        nexttrainerID=++trainerID;
    }

    public int getTrainerID(){      
        return nexttrainerID;
    }
}

This is my main class to run it:

autoTrainerID ID = new autoTrainerID();
int trainerID = ID.getTrainerID();
System.out.println("\t\tTrainer ID : T"+trainerID);`

try(PrintWriter TAdd = new PrintWriter(new BufferedWriter(
    new FileWriter("TrainersInfo.txt", true)))) {

    TAdd.print(trainerID);
    TAdd.println("");

    TAdd.close();
} 
catch(IOException e) {
    System.out.println("ERROR!");
}

I wanna make it can automatic track back the latest trainer ID:

Example: inside the saved text file got latest trainer ID is 100002, then i wan track it back and make it become 100003.

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
kennethwkz
  • 25
  • 2
  • 7

1 Answers1

0

You need to read the last entry from the file before you do the next read.

Refer Quickly read the last line of a text file? to find out how to read the last line (or entry)

Community
  • 1
  • 1
Maas
  • 1,317
  • 9
  • 20