Java 为什么这个代码循环16次?

Java 为什么这个代码循环16次?,java,file,loops,Java,File,Loops,可能重复: 代码如下: 读取文件方法: package textfiles; import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader; import java.util.List; import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; public

可能重复:

代码如下:

读取文件方法:

package textfiles;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.List;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFile {

private String path;

// create a method which takes in the classpath of the file
public ReadFile(String filePath) {
path = filePath;

}

public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);

List<String> textData = new LinkedList<String>();
String line;

while ((line = textReader.readLine()) != null) {
   Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL);
   Matcher matcher = pattern.matcher(line);
   line = matcher.replaceFirst("");
   if (line.trim().length()==0) continue;
   if (!line.startsWith("//")) { textData.add(line); }
   else if (!line.startsWith("(")) { textData.add(line); }





}

 // close the line-by-line reader and return the data
 textReader.close();
 return textData.toArray(new String[textData.size()]);
}
}
打包文本文件;
导入java.io.IOException;
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.util.List;
导入java.util.LinkedList;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类读取文件{
私有字符串路径;
//创建一个接受文件类路径的方法
公共读取文件(字符串文件路径){
路径=文件路径;
}
公共字符串[]OpenFile()引发IOException{
FileReader=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(读卡器);
List textData=newlinkedlist();
弦线;
而((line=textReader.readLine())!=null){
Pattern=Pattern.compile(“/.*$”,Pattern.DOTALL);
匹配器匹配器=模式匹配器(线);
line=matcher.replaceFirst(“”);
如果(line.trim().length()==0)继续;
如果(!line.startsWith(“/”){textData.add(line);}
如果(!line.startsWith(“(”){textData.add(line);}
}
//关闭逐行读卡器并返回数据
textReader.close();
返回textData.toArray(新字符串[textData.size()]);
}
}
我的主要方法是:

try {
    ReadFile files = new ReadFile(file.getPath());
    String[] anyLines = files.OpenFile();

    int i;



     //  test if the program actually read the file
     for (i=0; i<anyLines.length; i++) {
         int wordValue = 16;

         // to keep track words that are already used
         Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

         for (String line : anyLines) {

             // if line doesn't begin with &, then ignore it
             if (!line.startsWith("@")) {
                 continue;
             }

             // remove &
             line = line.substring(1);

             Integer binaryValue = null;

             if (line.matches("\\d+")) {
                 binaryValue = Integer.parseInt(line);
             }
             else if (line.matches("\\w+")) {
                 binaryValue = wordValueMap.get(line);

                // if the map doesn't contain the word value, then assign and store it

                 if (binaryValue == null) {
                     binaryValue = wordValue;
                      wordValueMap.put(line, binaryValue);
                     wordValue++;
                 }
           }

       // I'm using Commons Lang's StringUtils.leftPad(..) to create the zero padded string
       System.out.println(Integer.toBinaryString(binaryValue));


    }
试试看{
ReadFile files=新的ReadFile(file.getPath());
String[]anyLines=files.OpenFile();
int i;
//测试程序是否实际读取了该文件

对于(i=0;i您有两个嵌套循环,每个循环执行
anyLines.length
次。如果文件中有四行,内部循环将执行16次。

请修复您的代码格式。请查看我对其他相关线程所做的编辑,并解决它们提出的问题。@rudna1010您能发布所有代码吗?@trutheality,anyLines是一个具有文件长度的数组(本例中为19行)@GeorgeBecj:我想这是我们唯一关心的代码(我在上面发布了文件读取方法)。你能发布输出吗?这很尴尬:哦,你是对的!谢谢!现在唯一的问题是用二进制替换字符。