Java-将ArrayList的元素存储到单独的块中

Java-将ArrayList的元素存储到单独的块中,java,arraylist,block,bufferedreader,filereader,Java,Arraylist,Block,Bufferedreader,Filereader,这是我所有的代码,总之,它标准化了两个文本文件,然后打印出结果 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.pri

这是我所有的代码,总之,它标准化了两个文本文件,然后打印出结果

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

}
import java.io.*;
导入java.util.*;
公开课剽窃{
公共静态void main(字符串[]args){
剽窃myPlag=新剽窃();
如果(args.length==0){
System.out.println(“错误:没有文件输入”);
}
否则如果(args.length>0){
试一试{
对于(int i=0;i
我想实现的下一点是:使用命令行,第三个参数将是用户输入的任何整数(块大小)。然后我必须使用它将数组的元素存储到重叠的单独块中。猫坐在垫子上,大小为4块。块1将是:C块2:heca块3:ecat,依此类推,直到它到达阵列的末端

有什么想法吗


提前感谢各位。

您所要求的一切都可以通过字符串操作完成。首先使用replaceAll()删除空格,然后使用for循环和substring()创建块

对于for循环,您需要修改它,以便它读取两个文本,然后使用第3个参数作为块大小,以便将for循环更改为:

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

for(int i=0;i要获得块大小,请使用以下命令:

if(args.length != 4)
    return;
int blockSize = Integer.valueOf(args[3]);
这是一个可以帮助你的例子

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

这就是你想要做的吗?我们可以用多种方式编码,下面是一个例子

输入3个参数前2个是文件,第3个是块大小:

这是一个男孩

这是一个女孩

区块大小:4

预期产出:

这是希西·伊莎·伊莎伯·阿博·博伊特,这是希西·伊莎·伊莎格·萨吉·阿吉尔的女孩

节目:

导入java.io。; 导入java.util

公开课剽窃{

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;  
}
publicstaticvoidmain(字符串[]args){
//剽窃myPlag=新剽窃();
/*args=新字符串[3];
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入第一个文件路径”);
args[0]=s.next();
System.out.println(“输入第二个文件路径”);
args[1]=s.next();
System.out.println(“输入块的大小”);
args[2]=s.next()*/
int blockSize=Integer.valueOf(args[2]);
StringBuilder wholeContent=新StringBuilder(“”);
如果(args.length==0){
System.out.println(“错误:没有文件输入”);
}
否则如果(args.length>0){
试一试{
for(int i=0;ifor(int j=0;jI意识到这一点,但正在努力使用命令行参数来指定块大小?那么您的命令行参数目前看起来是什么样子?java剽窃(文本文件)(文本文件)(块参数将在此处)好的,那么问题是什么呢,你可以说java剽窃text1.txt text2.txt 5,这应该有效。第一个参数是程序名。因此循环应该从i=1开始,确保它是args[2],因为这是cmd中的第三个参数?当你像这样调用你的程序:myProgram arg1 arg2 arg3时,args数组将conatin:args[0]=myProgram,args[1]=arg1,args[2]=arg2,args[3]=arg3。啊,好的。另外,我使用了ArrayList,所以当我尝试使用substring()时出现了一个错误。您得到了什么错误?我忘记了括号是您得到的错误吗?不,找不到符号子字符串,因为我使用了list
[Thed, hedo, edog, dogi, ogis, gisi, isin, sint, inth, nthe, theh, heho, ehou, hous, ouse]
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;  
}