从void方法/函数返回值

时间:2019-03-16 01:17:55

标签: java arrays

我需要实现一个Java函数/方法,该函数可以在界面上将csv转换为数组,但是由于该函数的类型为void,因此它不会在界面上返回任何内容(尽管它可以在我的本地控制台上运行并打印出该数组)。

这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestImpl implements Test
{


    public void tricsv(String[] args) {

        String csvFile = "filetoconvert.csv";
        String line = "";
        String cvsSplitBy = ",";

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                String[] cell = line.split(cvsSplitBy);

                System.out.println("Result= " + cell[0]+ "");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

    public class TestImpl implements Test
    {
    private String[] cell;

     public String getValue(int index) {
return cell[index];
}
        public void tricsv(String[] args) {

            String csvFile = "filetoconvert.csv";
            String line = "";
            String cvsSplitBy = ",";

            try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

                while ((line = br.readLine()) != null) {

                    cell = line.split(cvsSplitBy);

                    System.out.println("Result= " + cell[0]+ "");

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }