Pregunta

so here is ALL of my code, which, in summary, standardises two text files then prints out the result.

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;  
    }

}

The next bit I want to implement is this: Using the command line, the 3rd argument will be any integer(size of blocks) which the user enters. I have to use this then to store the elements of that array into separate blocks which overlap. EG: The cat sat on the mat, block size 4. Block 1 would be: Thec Block 2: heca Block 3: ecat, and so on, until it reaches the end of the array.

Any ideas?

Thanks in advance guys.

¿Fue útil?

Solución

To get the block size use this :

if(args.length != 4)
    return;
int blockSize = Integer.valueOf(args[3]);

This an example that could help you

import java.util.*;

public class Test {
    public static void main(String[] args) {
    String line = "The dog is in the house";
    line = line.replace(" ", "");
    List<String> list = new ArrayList<String>();
    for (int i = 0; i <= line.length() - 4; i++) 
        list.add(line.substring(i, i + 4));
    System.out.println(list);

}

output :

[Thed, hedo, edog, dogi, ogis, gisi, isin, sint, inth, nthe, theh, heho, ehou, hous, ouse]

Is that what you want to do

Otros consejos

WE can code it in mulitple ways, here is one example.

Input 3 arguments first 2 are files and 3rd one is the block size:

File1 contain: this is a boy

File2 contain: this is a girl

block size: 4

Expected Output:

this hisi isis sisa isab sabo aboy boyt oyth ythi this hisi isis sisa isag sagi agir girl

Program:

import java.io.; import java.util.;

public class Plagiarism {

public static void main(String[] args) {
    //Plagiarism myPlag = new Plagiarism();

    /*args = new String[3];
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the 1st file path");
    args[0] = s.next();
     System.out.println("Enter the 2nd file path");
     args[1] = s.next();
     System.out.println("Enter size of block");
     args[2] = s.next();*/

    int blockSize = Integer.valueOf(args[2]);

    StringBuilder wholeContent = new StringBuilder("");


    if  (args.length == 0) {
        System.out.println("Error: No files input");
    }
    else if (args.length > 0) {
        try {
            for (int i = 0; i < args.length-1; 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));
                        wholeContent.append(foo.get(j));

                    }
            }


            System.out.println("The content of Line is = "+ wholeContent);
            System.out.println("The content of line based on the block size = "+ blockSize + " is:");
            for(int j=0; j<=(wholeContent.length()-blockSize); j++){
                            System.out.print(wholeContent.substring(j, j+4));
                            System.out.print(" ");
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            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) {
            if(!" ".equals(line))
        myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
    }
    return myList;  
}

}

All you are asking to do can be done with string manipulation. First use replaceAll() to remove your spaces, then use a for loop and substring() to create your blocks.

for your for loop you need to modify it so that it reads the two texts, then uses the 3rd argument as the block size so you would change your for loop from:

for(int i = 0; i<args.length;i++)

to:

for(int i = 1; i<3; i++)

this reads the first two arguments but not the third

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top