Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在没有第三方库的情况下读取CSV文件_Java_Csv_Arraylist_Stringtokenizer - Fatal编程技术网

Java 在没有第三方库的情况下读取CSV文件

Java 在没有第三方库的情况下读取CSV文件,java,csv,arraylist,stringtokenizer,Java,Csv,Arraylist,Stringtokenizer,我正在尝试将csv文件读入ArrayList或String[][]数组。在本文中,我试图将其读入一个列表,然后使用标记器将该列表形成一个数组。csv文件有7列(A-G)和961行(1-961)。我的2D数组for循环不断返回空指针,但我认为它应该可以工作 public class FoodFacts { private static BufferedReader textIn; private static BufferedReader foodFacts;

我正在尝试将csv文件读入ArrayList或String[][]数组。在本文中,我试图将其读入一个列表,然后使用标记器将该列表形成一个数组。csv文件有7列(A-G)和961行(1-961)。我的2D数组for循环不断返回空指针,但我认为它应该可以工作

public class FoodFacts
{
    private static BufferedReader textIn;
    private static BufferedReader foodFacts;
            static int numberOfLines = 0; 
            static String [][] foodArray;
    public static String  aFact;
    static  int NUM_COL = 7;
    static int NUM_ROW = 961;
    // Make a random number to pull a line
    static Random r = new Random();

    public static void main(String[] args)
    {
        try 
        {
            textIn = new BufferedReader(new InputStreamReader(System.in));
            foodFacts= new BufferedReader(new FileReader("foodfacts.csv"));
            Scanner factFile = new Scanner(foodFacts);
            List<String> facts = new ArrayList<String>();

            String fact;
            System.out.println("Please type in the food you wish to know about.");
            String request = textIn.readLine();
            while ( factFile.hasNextLine()){
                fact = factFile.nextLine();
                StringTokenizer st2 = new StringTokenizer(fact, ",");
                //facts.add(fact);
                numberOfLines++;
                while (st2.hasMoreElements()){
                    for ( int j = 0; j < NUM_COL ; j++) {
                        for (int i = 0; i < NUM_ROW ; i++){
                            foodArray [j][i]= st2.nextToken();  //NULL POINTER HERE
                            System.out.println(foodArray[j][i]);
                        }
                    }
                }
            }
        }
        catch (IOException e)
        {
            System.out.println ("Error, problem reading text file!");
            e.printStackTrace();
        }
     }
 }
公共类FoodFacts
{
私有静态缓冲阅读器文本输入;
私有静态缓冲区读取程序foodFacts;
静态int numberOfLines=0;
静态字符串[][]foodArray;
公共静态字符串aFact;
静态int NUM_COL=7;
静态整数行=961;
//做一个随机数来拉一条线
静态随机r=新随机();
公共静态void main(字符串[]args)
{
尝试
{
textIn=新的BufferedReader(新的InputStreamReader(System.in));
foodFacts=newbufferedreader(新文件阅读器(“foodFacts.csv”);
扫描仪factFile=新扫描仪(foodFacts);
列表事实=新的ArrayList();
串事实;
System.out.println(“请输入您想知道的食物。”);
字符串请求=textIn.readLine();
while(factFile.hasNextLine()){
fact=factFile.nextLine();
StringTokenizer st2=新的StringTokenizer(事实,“,”);
//事实。添加(事实);
numberOfLines++;
while(st2.hasMoreElements()){
对于(int j=0;j
将您的
foodArray
初始化为
foodArray=新字符串[NUM_ROW][NUM_COL]在使用它之前

而且,当您一次读取一行时,不需要对
循环进行内部

使用
numberOfLines
作为行:

        while ( factFile.hasNextLine() && numberOfLines < NUM_ROW){
                 fact = input.nextLine();
                 StringTokenizer st2 = new StringTokenizer(fact, ",")    ;
                 //facts.add(fact);
                while (st2.hasMoreElements()){
                  for ( int j = 0; j < NUM_COL ; j++) {
                    foodArray [numberOfLines][j]= st2.nextToken(); 
                    System.out.println(foodArray[numberOfLines][i]);
                 }
                }  
                 numberOfLines++;
            }

一个问题:将所有变量声明为静态类变量有什么特殊目的吗?其中大多数适合作为方法内部的局部变量,例如
numberOfLines

foodArray
初始化为
foodArray=newstring[NUM_ROW][NUM_COL]在使用它之前

而且,当您一次读取一行时,不需要对
循环进行内部

使用
numberOfLines
作为行:

        while ( factFile.hasNextLine() && numberOfLines < NUM_ROW){
                 fact = input.nextLine();
                 StringTokenizer st2 = new StringTokenizer(fact, ",")    ;
                 //facts.add(fact);
                while (st2.hasMoreElements()){
                  for ( int j = 0; j < NUM_COL ; j++) {
                    foodArray [numberOfLines][j]= st2.nextToken(); 
                    System.out.println(foodArray[numberOfLines][i]);
                 }
                }  
                 numberOfLines++;
            }

一个问题:将所有变量声明为静态类变量有什么特殊目的吗?其中大多数适合作为方法内部的局部变量,例如
numberOfLines

您可以使用此
String[]foodArray=csvreadString(文件名)方法。它实际上会读取文件两次,但我不知道如何在不读取数据的情况下获取csv维度(您需要维度来初始化数组),与我尝试的其他方法相比,这是非常快的

   static public class PairInt {

        int rows = 0;
        int columns = 0;
    }
   static PairInt getCsvSize(String filename) throws Throwable {
        PairInt csvSize = new PairInt();
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
        String line;
        while ((line = reader.readLine()) != null) {
            if (csvSize.columns == 0) {
                csvSize.columns = line.split(",").length;
            }
            csvSize.rows++;
        }
        reader.close();
        return csvSize;
    }
    static String[][] csvreadString(String filename) throws Throwable {
        PairInt csvSize = getCsvSize(filename);
        String[][] data = new String[csvSize.rows][csvSize.columns];
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
        for (int i = 0; i < csvSize.rows; i++) {
            data[i] = reader.readLine().split(",");
        }
        return data;
    }
静态公共类PairInt{
int行=0;
int列=0;
}
静态PairInt getCsvSize(字符串文件名)抛出Throwable{
PairInt csvSize=新的PairInt();
BufferedReader=new BufferedReader(new FileReader(new File(filename)));
弦线;
而((line=reader.readLine())!=null){
if(csvSize.columns==0){
csvSize.columns=line.split(“,”).length;
}
csvSize.rows++;
}
reader.close();
返回csvSize;
}
静态字符串[][]csvreadString(字符串文件名)抛出可丢弃的{
PairInt csvSize=getCsvSize(文件名);
字符串[][]数据=新字符串[csvSize.rows][csvSize.columns];
BufferedReader=new BufferedReader(new FileReader(new File(filename)));
对于(int i=0;i
您可以使用此
字符串[][]foodArray=csvreadString(文件名)方法。它实际上会读取文件两次,但我不知道如何在不读取数据的情况下获取csv维度(您需要维度来初始化数组),与我尝试的其他方法相比,这是非常快的

   static public class PairInt {

        int rows = 0;
        int columns = 0;
    }
   static PairInt getCsvSize(String filename) throws Throwable {
        PairInt csvSize = new PairInt();
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
        String line;
        while ((line = reader.readLine()) != null) {
            if (csvSize.columns == 0) {
                csvSize.columns = line.split(",").length;
            }
            csvSize.rows++;
        }
        reader.close();
        return csvSize;
    }
    static String[][] csvreadString(String filename) throws Throwable {
        PairInt csvSize = getCsvSize(filename);
        String[][] data = new String[csvSize.rows][csvSize.columns];
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
        for (int i = 0; i < csvSize.rows; i++) {
            data[i] = reader.readLine().split(",");
        }
        return data;
    }
静态公共类PairInt{
int行=0;
int列=0;
}
静态PairInt getCsvSize(字符串文件名)抛出Throwable{
PairInt csvSize=新的PairInt();
BufferedReader=new BufferedReader(new FileReader(new File(filename)));
弦线;
而((line=reader.readLine())!=null){
if(csvSize.columns==0){
csvSize.columns=line.split(“,”).length;
}
csvSize.rows++;
}
reader.close();
返回csvSize;
}
静态字符串[][]csvreadString(字符串文件名)抛出可丢弃的{
PairInt csvSize=getCsvSize(文件名);
字符串[][]数据=新字符串[csvSize.rows][csvSize.columns];
BufferedReader=new BufferedReader(new FileReader(new File(filename)));
对于(int i=0;i
您希望阅读几种称为“CSV”的格式中的哪一种?它在excel中?逗号分隔值您必须在没有第三方库的情况下执行此操作吗?教师命令。他喜欢让事情变得特别令人沮丧解析CSV文件并不像你(或者你的老师)可能相信的那么简单。查看以下问题列表的答案:以下哪项是sev