0

嘿,我需要代码在行尾替换 txt 文件中所有出现的连词。我有一个保存在 txt 文件中的连词列表。我希望输入文件和连词文件都以数组形式存储。然后使用 for 循环我想比较两个数组。但这会产生很多错误。是否存在更好的方法来做同样的事情?这是我尝试做的,但它在 for 循环中显示错误

import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
public class Toarray
    {
    private static Object arrays;
    public static void main(String args[]) throws FileNotFoundException
     {
      String filename,path;
      System.out.println("select the input file");
      JFileChooser chooser = new JFileChooser();
      chooser.showOpenDialog(null);               
      File file1 = chooser.getSelectedFile();               
                chooser.showOpenDialog(null);           

                 filename = file1.getName();
                path= file1.getPath();

                Scanner sc;
                sc = new Scanner(new File(filename));
                List<String> lines = new ArrayList<String>();
                while (sc.hasNextLine()) {
                    lines.add(sc.nextLine());
                }

               String[] inp = lines.toArray(new String[0]);

               for (int index=0;index<=20;index++ ){
System.out.println(inp[index]);}

               String remove;
                remove="/Machintosh HD/Users/vaishnavi/Desktop/temp.txt";
                Scanner sc1;
                sc1 = new Scanner(new File(remove));
                List<String> con;
                con = new ArrayList<String>();
                while (sc1.hasNextLine()) {
                     lines.add(sc1.nextLine());
                }

               String[] conj = con.toArray(new String[0]);      
             }
     StriString oldtext;
         for(int i=0;i<=55;i++)

                 {
                  for(int j=0;j<=75;j++)
                         {
                        if(  inp[i].equals(conj[j]))
                        {
                             String newtext = oldtext.replaceAll(inp[i], ".");
                                FileWriter writer = null;   
                                 try {
                                   writer = new FileWriter(path);
                                    } catch (IOException ex) {
                                        Logger.getLogger(Toarray.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                 try {
                                     writer.write(newtext);
                                 } catch (IOException ex) {
                                     Logger.getLogger(Toarray.class.getName()).log(Level.SEVERE, null, ex);
                                 }
            }
        }
    }
}

请帮忙:)

4

1 回答 1

0

要检查数组是否包含某些内容,请尝试以下操作:

if(Arrays.asList(inp).contains("something")){

    //Do something
}
于 2013-04-01T09:25:16.980 回答