0

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
J. Smith
  • 17
  • 1
  • 3
  • 1
    If you are on `Windows` try run the program as Administrator.. – Ramesh-X Dec 15 '15 at 04:36
  • 1
    `You are not allowed to read the file ...` Seems pretty clear and unambiguous to me... – John3136 Dec 15 '15 at 04:36
  • Do you have a class definition? The code you have provided will not compile. It also seems pretty clear what the error is... – mkobit Dec 15 '15 at 04:37
  • @mkobit It says not to submit a whole Java class, just a method. No matter what I call the text file, it won't go through. I know it seems so obvious, I just can't figure out how to fix it. – J. Smith Dec 15 '15 at 04:48

4 Answers4

1

It looks like you are using Tomcat from your path. Tomcat requires special security permission to read or write files. This is a basic protection to prevent malicious code from accessing sensitive files on the OS. You can configure these directories or stick to reading and writing to the default ones:

https://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html

John Scattergood
  • 1,032
  • 8
  • 15
1

Unable to add a comment because of reputation points so using the Answers section.

Agree with above comments, it is related to permissions.

  1. Do an ls -ltr on /usr/share/tomcat7/temp/idontknowwhattodo.txt
  2. Check whether the user (say myuser) with which you are running you java application has necessary permissions for /usr/share/tomcat7/temp/idontknowwhattodo.txt.
  3. Two options below:
  4. Give the user "myuser" the necessary permissions to the idontknowwhattodo.txt using chmod.
  5. Or copy idontknowwhattodo.txt to a location where "myuser" has the permissions.
  • That's not the problem he is trying to solve, obviously server would be running in a restricted user mode. However your answer gives insight at how it's working on the server side, +1 for that – 11thdimension Dec 15 '15 at 04:58
  • @Carpetsmoker Valid point. I do agree that using `chmod 777` is not a good practice or recommendation. Updated the answer accordingly. – Madhusudana Reddy Sunnapu Mar 13 '16 at 05:30
0

As per instructions, you are already getting the Scanner object(which references the input file) as parameter to your method. So, you should not be re-initializing it. This line should be removed:

 input = new Scanner(new File("idontknowwhattodo.txt"));
Manish
  • 3,913
  • 2
  • 29
  • 45
0

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()) {
 ...
11thdimension
  • 10,333
  • 4
  • 33
  • 71