Reputation: 99
Here is all of my code for you guys to see, I will explain the problem below:
import java.io.*;
import java.util.*;
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]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}
public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();
while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
}
return myList;
}
}
Right, what this does is print out this:
abcdefabcd123abcdz456aabcdd
This result is actually two separate documents, text1.txt
and text2.txt
combined into one line on the cmd. What I need is these two documents to be standardised and then printing out as seperate elements for each value they each hold, rather than all together as one, because I need to then use them as 2 separate things (or objects) for the next part of my code.
Hopefully this is not too confusing and you clever people can help me out! Thanks in advance!
Upvotes: 0
Views: 64
Reputation: 2009
If you want to hold the file contents in two different objects do the following..
List<ArrayList<String>> files = new ArrayList<ArrayList<String>>();
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
files.add(simplify(reader));
for(ArrayList<String> al : files)
{
for (int j = 0; j < al.size(); j++) {
System.out.print(al.get(j));
}
System.out.println();
}
}
and change your second method to
public static ArrayList<String> simplify(BufferedReader input) throws IOException
Upvotes: 0
Reputation: 1372
check this one will print data line by line
abcdefabcd123
abcdz456aabcdd
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]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
System.out.println();
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}
public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();
while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
}
return myList;
}
}
Upvotes: 2
Reputation: 464
You are just printing out the result, so as it runs through the loop, it will print it all out on one line.
You could store the results in a separate array, or you could just do this to have them each print on a different line:
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
System.out.println();
}
The new println will separate each entry.
Upvotes: 2
Reputation: 993
It gets printed into one line because you use System.out.print(), you could just use System.out.println() if you want to start a new line with each output.
But that is just a problem of the output. Internally you already have the data separated in your list, so what is your actual problem?
Upvotes: 0