1

So I have the following code:

import java.io.*;

public class Plagiarism {

    public static void main(String[] args) {

        Plagiarism myPlag = new Plagiarism();

        if  (args.length == 0) {
            System.out.println("Error: No files input");
        }
        else if (args.length > 0) {
            try {
                for (int i = 0; i < args.length; i++) {
                    BufferedReader reader = new BufferedReader (new FileReader (args[i]));
                    simplify (reader);
                    reader.close();
                }
            }
            catch (Exception e) {
                System.err.println ("Error reading from file");
            }
        }
    }

    public static void simplify(BufferedReader input) throws IOException {
        String line = null;

        line = input.readLine();
        while (line != null) {
            line = line.replaceAll ("[^a-zA-Z0-9 ]", "");
            line = line.toLowerCase();
        }    
    }

}

The problem with this code is that it compiles, but when i run it and add in the 2 arguments in command line eg. Java Plagiarism text1.txt text2.txt. EDIT: When I run this it just doesn't do anything or even finish, it's like it's stuck somewhere.

Thanks for any help.

5
  • add System.out.println(e.getMessage()); in your catch clause to learn what the exception is. Commented Mar 13, 2014 at 14:50
  • 3
    Start by not catching Exception but a more specific one. Exception also catches RuntimeException and derivates, therefore all unchecked exceptions. Commented Mar 13, 2014 at 14:50
  • Print your Stacktrace (e.printStackTrace()). Commented Mar 13, 2014 at 14:51
  • Where are your text1.txt and text2.txt?. Are you getting FileNotFoundException? Commented Mar 13, 2014 at 14:54
  • Sorry guys I edited the question. The text files are in the same directory. Commented Mar 13, 2014 at 14:54

2 Answers 2

1

You are not reading the files at once (you would need to use threading to do that).

The issue is in your simplify method.

line = input.readLine();
while (line != null) {

... should become:

while ((line = input.readLine()) != null)

The reason for this is that you are calling readLine only once, and iterating only over the first line's value otherwise.

With the proper while loop you pipe the value of the readLine call to the non-null condition after assigning it to your line variable.

You can then do whatever you want with the line String you manipulated in your while loop, such as adding it to an array or Collection, as you suggested in your comment.

For instance:

public static List<String> simplify(BufferedReader input) throws IOException {
    String line = null;
    List<String> result = new ArrayList<String>();

    while ((line = input.readLine()) != null) {
        result.add(line.replaceAll ("[^a-zA-Z0-9 ]", "").toLowerCase());
    }    
    return result;
}

... then in your main method...

List<String> foo = simplify(reader);
Sign up to request clarification or add additional context in comments.

12 Comments

I appreciate your answer. With the second issue though, how do you mean? This code should surely work? If it cannot work how can I get it to work with the readers? Thanks
I see. I don't want to EDIT the file though - I'd want to put the 2 text documents into arrays first then edit those arrays, which is what im attempting.
@user3364788 aha my bad. Then you don't need writers. Let me edit again.
Surely "line" should be filled though after this while loop? When I try to print "line" it's still null...
@user3364788 it's filled inside the while loop! Each line you read until line is null. Outside the loop it will be null as null equality is the loop-exiting condition.
|
0

EDIT: When I run this it just doesn't do anything or even finish, it's like it's stuck somewhere.

Which is perfectly normal. Look at your while loop:

    while (line != null) {
        line = line.replaceAll ("[^a-zA-Z0-9 ]", "");
        line = line.toLowerCase();
    }

line will never be null here. You should:

while ((line = input.readLine()) != null)
    // etc 

You have a more fundamental problem: your program will never work correctly if your goal is to replace lines in the input files...

Try this instead:

private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9 ]+");

private static void simplify(final String fileName)
    throws IOException
{
    final Path path = Paths.get(fileName);
    final Path tempfile = Files.createTempFile(fileName.getFileName(), "tmp");
    try (
        final BufferedReader reader = Files.newBufferedReader(path);
        final BufferedWriter writer = Files.newBufferedWriter(tempfile);
    ) {
        String line;
        while ((line = reader.readLine()) != null) {
            line = PATTERN.matcher(line).replaceAll("").toLowerCase();
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
    }
    Files.move(tempfile, path, StandardCopyOption.REPLACE_EXISTING);
}

1 Comment

Thanks for your answer. This isn't what I need though I don't think. Fundamentally I am comparing both the text documents, so I am not replacing the content in the files, but instead using it and then editing the content in arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.