Search code examples
javasecurityexception

Java - SecurityException in Method "printDuplicates"


I'm a newcomer to Java trying to submit a working project, in this instance printDuplicates. The instructions are as follows:

Write a method named printDuplicates that accepts as its parameter a Scanner for an input file containing a series of lines. Your method should examine each line looking for consecutive occurrences of the same token on the same line and print each duplicated token along how many times it appears consecutively. Non-repeated tokens are not printed. Repetition across multiple lines (such as if a line ends with a given token and the next line starts with the same token) is not considered in this problem.

For example, if the input file contains the following text:

hello how how are you you you you I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge bow wow wow yippee yippee yo yippee yippee yay yay yay one fish two fish red fish blue fish It's the Muppet Show, wakka wakka wakka Your method would produce the following output for the preceding input file:

how*2 you*4 I*3 Jack's*2 smirking*5 wow*2 yippee*2 yippee*2 yay*3

wakka*3 Your code prints only the repeated tokens; the ones that only appear once in a row are not shown. Your code should place a single space between each reported duplicate token and should respect the line breaks in the original file. This is why a blank line appears in the expected output, corresponding to the fourth line of the file that did not contain any consecutively duplicated tokens. You may assume that each line of the file contains at least 1 token of input.

Here is my code, pretty much ready for submitting.

import java.io.*;
import java.util.*;
Scanner input;
public static void printDuplicates(Scanner input) throws Exception {
    String word = "";
    String word2 = "";
    input = new Scanner(new File("idontknowwhattodo.txt"));
    while(input.hasNextLine()) {
        Scanner line = new Scanner(input.nextLine());
        int repeat = 1;
        word = line.next();
        while(line.hasNext()) {
word2 = line.next();
while(word.equals(word2)) {
    repeat++;
    if(line.hasNext()){
        word2 = line.next();
    } else {
        break;
    }
}
if(repeat!=1) {
    System.out.print(word + "*" + repeat + " ");
}
repeat = 1;
word = word2;
}
        System.out.println();
    }
}

However, whenever I try to submit my project, it throws back this error:

(no output was produced!)
SecurityException on line 5:
You are not allowed to read the file       /usr/share/tomcat7/temp/idontknowwhattodo.txt

java.lang.SecurityException: You are not allowed to read the file   /usr/share/tomcat7/temp/idontknowwhattodo.txt
at java.io.FileInputStream.<init>(FileInputStream.java:135)
at java.io.FileReader.<init>(FileReader.java:72)
at Scanner.<init>(Scanner.java:330)
at printDuplicates (Line 5)

What does this mean? I have multiple working projects but I can't seem to submit them due to this one error. Any experts that can help me on this one? Thank you.


Solution

  • Problem description says that you're getting Scanner object as parameter. You don't have to recreate it, you're probably trying to submit your project to some online competition. Program on the server will load your class and call the method printDuplicates() with Scanner object as parameter, you don't have to worry about how it gets created. Just use it, and everything would be fine.

    Just comment the scanner assignment line as below

    String word = "";
    String word2 = "";
    /*input = new Scanner(new File("idontknowwhattodo.txt"));*/
     while(input.hasNextLine()) {
     ...