I need to retrieve and delete a random line from a txt file (the same line). Thus far I've come up with the following code:
 public String returnAndDeleteRandomLine(String dir) throws FileNotFoundException, IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(dir))) {
        //StringBuilder sb = new StringBuilder();
        //System.out.println("Value of line before while() " + line);
        ArrayList fileContents = new ArrayList();
        int maxLines = 0;
        String line = br.readLine();
        //System.out.println("Value of line before while() " + line);
        while (line != null) {
            fileContents.add(line.toString());
            line = br.readLine();
            //System.out.println("Value of line is: " + line);
        }
        System.out.println("Value of maxLines() " + maxLines);
        Random rand = new Random();
        int randomNumber = rand.nextInt(maxLines - 1) + 1;
        System.out.println("Value of randomNumber: " + randomNumber);
        int lineNumber = randomNumber;
        if (fileContents.isEmpty()) {
            return null;
        } else System.out.println("Value of random line: " + fileContents.get(randomNumber).toString());
        return fileContents.get(randomNumber).toString();
    }
 }
But I keep getting different errors. The most recent error was:
Value of maxLines() 0 Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Unknown Source) at TransmitToFile.returnAndDeleteRandomLine(TransmitToFile.java:247) at Main.main(Main.java:98)
I could not even work on deleting the line because I'm still unable to retrieve the line.
 
     
     
     
     
     
    