OpenCSV read specific column - android

i am facing small problem while using OPENCSV and trying to read specific column from a line. I have a csv file that looks like this
"ID","Name","Name2","Date","Author"
"1","Alex","Example","18.3.2016","Alex"
Now i want to read only the column 2 and 3 (Name and Name2).
My code looks like this
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
for(int i = 0; i< nextLine.length ; i++){
System.out.println("Cell index: " + i);
System.out.println("Cell Value: " + nextLine[i]);
System.out.println("---");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I already tried setting the "i" variable manually to 1 and 2. But then i am getting 4 same results shown in log. What is missing? Thanks!

Never mind, i found the trick.
Here is how it should look.
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
String name = nextLine[1];
String name2 = nextLine[2];
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After it, i could get the string value and work with it.

Related

Go to newline in android with HTMLViewer

I will store a data in a file.and i will display it.But my problem is how can i control the HTMLVewr or other reader to go a newline.
in this code when i display the file "file1" there are no newline.
try {
FileOutputStream ops=openFileOutput("file1",Context.MODE_PRIVATE);
DataOutputStream dos=new DataOutputStream(ops);
for(int i=0;i<w*h;i++){
//int alpa=Color.alpha(pixels[i]);
int red=Color.red(pixels[i]);
int green=Color.green(pixels[i]);
int blue=Color.blue(pixels[i]);
String k;
if(i==0)
k=red+" "+green+" "+blue+" ";
else if((i%(w-1)==0)&&(i>0)){
k="**"+red+" "+green+" "+blue+" ";
k=k+"/n";}
else
k="**"+red+" "+green+" "+blue+" ";
dos.write(k.getBytes());
}
ops.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to convert Shift-JIS encoded string to UTF-8?

I am getting html source from Aozora Bunko. Html file is Shift-JIS encoded. I am trying to get book title and author. Then I want to record title and author into SQLite(UTF-8) database.
String[] splittedResult = result.split("\"title\">");
splittedResult = splittedResult[1].split("</h1>");
String title = splittedResult[0];
byte[] b = null;
try {
b = title.getBytes("Shift_JIS");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String value=null;
try {
value = new String(b, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
...
myDatabase.addBookInformation(value, author);
Result is like this: latin letters are showing normally. But japanese letters are shown by blocks question mark inside (please do not pay attention to null values)
How to solve this problem?
As #Codo pointed out, solution for this problem was before.
I changed this
s = EntityUtils.toString(response.getEntity(), "UTF-8");
to this
s = EntityUtils.toString(response.getEntity(), "Shift_JIS");
And now there is no need for encoding.
String[] splittedResult = result.split("\"title\">");
splittedResult = splittedResult[1].split("</h1>");
String title = splittedResult[0];
/** I HAVE TAKEN THIS PART OF MY CODE
byte[] b = null;
try {
b = title.getBytes("Shift_JIS");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String value=null;
try {
value = new String(b, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
**/

fast read of blobs within SQLite using simpleQueryForBlobFileDescriptor?

I am reading blobs with size between 100kb and 1000kb from SQlite in my Android App using the following code :
public Object getCachefromDb(String sIdentifier){
String sSQL = " Select cache from cachtable where identifier='" + sIdentifier + "'";
Cursor c = null;
try {
c = connection_chronica.rawQuery(sSQL, null);
} catch (SQLiteException e) {
Log.v("SQLite Excetion", e.getMessage());
}
c.moveToFirst();
Log.v("DEBUG load Cache","sIdentifier : " + sIdentifier);
byte[] bData=null;
try{
bData = c.getBlob(0);
}catch(Exception e){
e.printStackTrace();
}
Object o = null;
if (bData!=null){
ByteArrayInputStream bos = new ByteArrayInputStream(bData);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(bos);
o=ois.readObject();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.close();
return o;
}
I would like to optimize the speed of reading and I found articles mentoining simpleQueryForBlobFileDescriptor.
My question : Does this help me reading BLOBS faster ? and if so how can I use it ?
Example from other posts:
SQLiteStatement get = mDb.compileStatement(
"SELECT blobColumn" +
" FROM tableName" +
" WHERE _id = 1" +
" LIMIT 1"
);
ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor()); // read like any other
My Tests results says its slower.
After long testing I found out, that using simpleQueryForBlobFileDescriptor is slower. See following code. My old code for example reads a blob in 390 miliseconds and the new code with simpleQueryForBlobFileDescriptor reads the same blob in 805 ms. I read somewhere that
simpleQueryForBlobFileDescriptor should be very fast for blob readings, but this seems not to be in my tests. Perhabs I am not doing it properly ? ( I hope so ). Any other hints.
public Object getCachefromDb_old(String sIdentifier){
Log.v("DEBUG LOAD BLOB","Start : " + sIdentifier);
Object o = null;
try {
// Erstelle ein SQLStatemant mit einer InClause
String sSQL = " Select cache from cachetable where identifier='" + sIdentifier + "'";
SQLiteStatement get = connection_chronica.compileStatement(sSQL);
ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor());
ObjectInputStream inStream = new ObjectInputStream(fis);
o=inStream.readObject();
} catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("DEBUG LOAD BLOB","End : " + sIdentifier);
return o;
}

Button text and string from text file (from asset) should be the same, but not in Android

I would like to make a quiz program. The questions are in a text file in asset folder. The answers are also in the asset folder called the number of the question (for example: the first question answers are in the text file called 1). I would like to give the questions and answers randomly (answers to a button). Until this everything is all right (maybe not the shortest solution, but works well). Then the user can answer the question clicking the correct button. And here is the problem. I get the text of the button and the first row of the answer file (always the first row is the right answer in the answer text file). It should be the same, and then I sign, this is the correct answer. But it's not the same, and I don't know why. A put text to the button from the answer file and get the first row from the answer file, so it should be the same. I print it out to log cat, and look like they are the same. I don't know what could be went wrong.
Can anybody help me out.
This is where I set the text of the button (randomly) and compare the first rows and the text of the button:
BufferedReader br2 = new BufferedReader(is2, 8192);
for(int k2=0; k2<3; k2++){
try {
kerdes2[k2] = br2.readLine();
final ArrayList <Integer> kerdesno2 = new ArrayList<Integer>();
for(int j=0;j<3;j++) kerdesno2.add(j);
Collections.shuffle(kerdesno2);
System.out.println(kerdesno2);
answ.setText(kerdes2[kerdesno2.get(0)]);
answ2.setText(kerdes2[kerdesno2.get(1)]);
answ3.setText(kerdes2[kerdesno2.get(2)]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
answ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ.getText().toString();
System.out.println(gomb);
for(int f=0; f<helyes.length(); f++)
{
char c = helyes.charAt(f);
char d = gomb.charAt(f);
if(c != d){
System.out.println(c);
System.out.println(((String) gomb).indexOf(c));
}
}
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ2.getText().toString();
System.out.println(gomb);
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String gomb = answ3.getText().toString();
String helyes = br3.readLine();
System.out.println(gomb);
System.out.println(helyes);
if(gomb == helyes){
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
As You can see I try to iterate over the two strings to realize where the problem is, but I couldn't manage to find...
String is an object. When comparing objects, use .equals(), not ==.
Your code:
if(gomb == helyes)
Should be:
if(gomb.equals(helyes))
By using == you're comparing memory, not the actual String objects. Sometimes you'll get the expected result, but other times you won't. .equals() will always test the Strings themselves.
I can see you are comparing by
if(gomb == helyes){
while it should be
if(gomb.equals(helyes)){

File reading not working for android

I am an android newbie and wrote this code for file handling but for some reason i am always getting back null values from the file. I also tried using readline() but got the same result. Would appreciate any help.
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String file = "test123";
try
{
OutputStream out = v.getContext().openFileOutput(file, MODE_PRIVATE);
InputStream in = v.getContext().openFileInput(file);
WriteFile(out);
String str = ReadFile(in);
Toast.makeText(v.getContext(), str, Toast.LENGTH_LONG);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public static void WriteFile(OutputStream out)
{
OutputStreamWriter tmp = new OutputStreamWriter(out);
try
{
for (int i = 0 ; i < 10; i++)
{
tmp.write(i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String ReadFile(InputStream in)
{
InputStreamReader tmp = null;
String str = "";
tmp = new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
try
{
for (int i = 0; i < 10; i++)
{
str += " " + reader.readLine();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
String file = "test123";
So, the path of your file should be {root}/test123
Try defining a path were you can access to see if it has written something. (usually : /mnt/storage/your_file)
Then, you'll be able to determine if the Write/Read process works or not
Note : Take a look at FileOutputStream, it already implements lots of useful methods

Categories

Resources