java中包含while循环的调用方法 public List CsvReaderGeneral(字符串CSVPATH,字符串AddStatement)抛出IOException{ BufferedReader BufferedReader=新的BufferedReader(新文件读取器(CSVPATH)); 字符串输入; 整数计数=0; 而((input=bufferedReader.readLine())!=null){ 计数++; } System.out.println(“计数:+Count”); BufferedReader br=新的BufferedReader(新文件读取器(CSVPATH)); br.readLine(); 字符串行=null; 列表l=新的ArrayList(); //字符串行=”; 而((line=br.readLine())!=null){ 字符串[]cols=line.split(“,”); l、 添加(添加语句); } 系统输出打印LN(l); 返回l; }

java中包含while循环的调用方法 public List CsvReaderGeneral(字符串CSVPATH,字符串AddStatement)抛出IOException{ BufferedReader BufferedReader=新的BufferedReader(新文件读取器(CSVPATH)); 字符串输入; 整数计数=0; 而((input=bufferedReader.readLine())!=null){ 计数++; } System.out.println(“计数:+Count”); BufferedReader br=新的BufferedReader(新文件读取器(CSVPATH)); br.readLine(); 字符串行=null; 列表l=新的ArrayList(); //字符串行=”; 而((line=br.readLine())!=null){ 字符串[]cols=line.split(“,”); l、 添加(添加语句); } 系统输出打印LN(l); 返回l; },java,while-loop,Java,While Loop,我在这里调用上述方法: public List<String> CsvReaderGeneral(String CSVPATH,String AddStatement) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new FileReader(CSVPATH)); String input; int count = 0; while((input = bu

我在这里调用上述方法:

public List<String> CsvReaderGeneral(String CSVPATH,String AddStatement) throws IOException{
    BufferedReader bufferedReader = new BufferedReader(new FileReader(CSVPATH));
    String input;
    int count = 0;
    while((input = bufferedReader.readLine()) != null)    {
        count++;
    }

    System.out.println("Count : "+count);
    BufferedReader br = new BufferedReader(new FileReader(CSVPATH));
    br.readLine();
    String line=null;
    List<String> l = new ArrayList<String>();
     // String line="";
    while ((line = br.readLine()) != null) {
        String[] cols = line.split(",");
        l.add(AddStatement);
    }
    System.out.println(l);
    return l;
}
public List CsvReaderIDStationSystemHealth()引发异常{
//返回CsvReaderGeneral(stationcsvpath、stationscolumnvalue);
返回CsvReaderGeneral(stationcsvpath,“cols[0]”);
}
我的输出如下所示:

计数:101
cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]cols[0]0]cols[0,cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0],cols[0]

它正在打印
cols[0]
,而不是该位置的值

如何获取值?是否有其他方法调用此方法?

AddStatement=“cols[0]”并且您只是在循环中打印出来,而不以任何方式使用变量。我将假设您正在尝试将行的第一个元素添加到“l”变量。因此,您的while循环应该如下所示

public List<String> CsvReaderIDStationSystemHealth() throws Exception{

        //return CsvReaderGeneral(STATIONSCSVPATH, STATIONSCOLOUMNVALUE);
        return CsvReaderGeneral(STATIONSCSVPATH, "cols[0]");
    }
您不能(轻松地)使用输入字符串调用动态创建的变量。如果您调用方法希望具有可调整的COL,那么您的方法头应该是

while ((line = br.readLine()) != null) {
        String[] cols = line.split(",");
        l.add(cols[0]);
    }

有时我必须使用while((line=br.readLine())!=null){String[]cols=line.split(“,”;l.add(“****”+cols[4];}]),这就是为什么我要添加到自定义
public List<String> CsvReaderGeneral(String CSVPATH,int colNum)
while ((line = br.readLine()) != null) {
        String[] cols = line.split(",");
        l.add(cols[colNum]);
    }