Reputation: 99
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.
Upvotes: 1
Views: 685
Reputation: 48404
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);
Upvotes: 1
Reputation: 121730
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);
}
Upvotes: 0