Reputation: 109
I am using OpenCSV in an Android Java project with IntelliJ.
I am trying to run the following code to create a bean inside my project. Note the segment surrounded by "** **"
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.opencsv.bean.CsvToBeanBuilder;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
**List<Restrictions> restrictionData = new CsvToBeanBuilder(FileReader("restrictions.csv"))
.withType(Restrictions.class).build.parse();**
}
}
I have taken this code from OpenCSV's site where they say:
Here we simply name the fields identically to the header names. After that, reading is a simple job:
List beans = new CsvToBeanBuilder(FileReader("yourfile.csv")) .withType(Visitors.class).build().parse();
This will give you a list of the two beans as defined in the example input file. Note how type conversions to basic data types (wrapped and unwrapped primitives, enumerations, and Strings) occur automatically.
My code resembles their same style. Here is the error I get:
C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:34: error: cannot find symbol
List<Restrictions> restrictionData = new CsvToBeanBuilder(FileReader("restrictions.csv"))
^
symbol: method FileReader(String)
location: class MainActivity
I have tried putting 'new' in front of FileReader, and when I do this I get THIS error:
C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:35: error: cannot find symbol
.withType(Restrictions.class).build.parse();
^
symbol: variable build
location: class CsvToBeanBuilder
I have OpenCSV and beanutils in my library, both implemented in my build.gradle, and my file restrictions.csv is in my app's src folder. My Restrictions class also has no errors and follows OpenCSV's bean setup to a tee.
What am I doing wrong? Any help appreciated, thank you!
Upvotes: 0
Views: 3282
Reputation: 1023
Use any of the below constructors for CsvToBeanBuilder. Constructor you are using is not valid :-
CsvToBeanBuilder(CSVReader csvReader)
CsvToBeanBuilder(Reader reader)
example :-
CSVReader csvReader = new CSVReader(new FileReader("restrictions.csv"));
List<Restrictions> restrictionData = new CsvToBeanBuilder(csvReader)
.withType(Restrictions.class)
.build()
.parse();
Upvotes: 1