java - Using Jackcess to retrieve numeric values stored in a text field gives ClassCastException -


i working jackcess read , categorize access database. it's meant open database, loop through each line, , print out individual row data console meet conditions. works fine, except when try read numeric values. code below. (this code built swing gui , gets executed when jbutton pressed.)

if (inv == null) {                  // check see if inventory file has been set.  if not, set default reference path.         inv = rpath;                         }  if (inventoryfile.exists()) {                   // check see if reference path exists.         list<string> testtypes = jlist1.getselectedvalueslist();         list<string> evaltypes = jlist3.getselectedvalueslist();         list<string> graintypes = jlist2.getselectedvalueslist();          stringbuilder sb = new stringbuilder();         (int i=0; i<=evaltypes.size()-1; i++) {             if (i<evaltypes.size()-1) {                 sb.append(evaltypes.get(i)).append(" ");             }             else {                 sb.append(evaltypes.get(i));             }         }         string evaltype = sb.tostring();          try (database db = databasebuilder.open(new file(inv));) {              table samplelist = db.gettable("ntep samples list");             cursor cursor = cursorbuilder.createcursor(samplelist);              (int i=0; i<=testtypes.size()-1; i++) {                 if ("sample volume".equals(testtypes.get(i))) {                     if (graintypes.size() == 1 && "hrw".equals(graintypes.get(0))) {                         switch (evaltype) {                             case "gmm":                                 (row row : samplelist){                                                                             if (null != row.getstring("currentgac")) {}                                     if ("hrw".equals(row.get("grain")) && row.getdouble("currentgac")>=12.00) {                                         system.out.print(row.get("grain") + "\t");                                         system.out.println(row.get("currentgac"));                                     }                                 }                                                                     break;                             case "nirt":                                 // conditional code                                 break;                             case "tw":                                 // more code                                 break;                          }                     }                     else {                         joptionpane.showmessagedialog(null, "only hrw samples can used selected test(s).", "error", joptionpane.error_message);                     }                                             break;                 }              }         }         catch (ioexception ex) {             logger.getlogger(samplefiltergui.class.getname()).log(level.severe, null, ex);         } 

when code run following error:

java.lang.classcastexception: java.lang.string cannot cast java.lang.double

the following condition looks throwing error.

row.getdouble("currentgac")>=12.00

it appears when data read database, program reading string, though fields numeric. attempting cast field double, java doesn't seem that. have tried using double.parsedouble() , double.valueof() commands try converting value (as mentioned here) without success.

my question is, how can convert these fields numeric values? trying type cast way go, or there different method i'm not aware of? notice in code created cursor, not using it. original plan use navigating through database, found example code jackcess webpage , decided use instead. not sure if right move or not, seemed simpler solution. appreciated. thanks.

edit:

to ensure program reading string value database, input following code

row.get("currentgac").getclass().getname()

the output java.lang.string, confirms string. suggested, changed following code

case "gmm":     (row row : samplelist){         if (null != row.get("currentgac"))          //system.out.println(row.get("currentgac").getclass().getname());         system.out.println(string.format("|%s|", row.getstring("currentgac")));          /*if ("hrw".equals(row.get("grain")) && row.getdouble("currentgac")>=12.00 && row.getdouble("currentgac")<=14.00) {              system.out.print(row.get("grain") + "\t");              system.out.println(row.get("currentgac"));         }*/                                             }                                         break; 

the ouput console these changes below

|9.85| |11.76| |9.57| |12.98| |10.43| |13.08| |10.53| |11.46| ...

this output, although looks numeric, still of string type. when tried run conditional statement (which commented out in updated sample code) still same java.lang.classcastexception error getting before.

jackcess not return values strings. retrieve fields (columns) of table appropriate java type access field type. example, test table named "table1" ...

id  doublefield  textfield --  -----------  ---------  1         1.23  4.56      

... following java code ...

table t = db.gettable("table1"); (row r : t) {     object o;     double d;     string fieldname;      fieldname = "doublefield";     o = r.get(fieldname);     system.out.println(string.format(             "%s comes as: %s",              fieldname,              o.getclass().getname()));     system.out.println(string.format(             "value: %f",              o));     system.out.println();      fieldname = "textfield";     o = r.get(fieldname);     system.out.println(string.format(             "%s comes as: %s",              fieldname,              o.getclass().getname()));     system.out.println(string.format(             "value: %s",              o));     try {         d = r.getdouble(fieldname);     } catch (exception x) {         system.out.println(string.format(                 "r.getdouble(\"%s\") failed - %s: %s",                  fieldname,                  x.getclass().getname(),                 x.getmessage()));     }     try {         d = double.parsedouble(r.getstring(fieldname));         system.out.println(string.format(                 "double.parsedouble(r.getstring(\"%s\")) succeeded. value: %f",                  fieldname,                  d));     } catch (exception x) {         system.out.println(string.format(                 "double.parsedouble(r.getstring(\"%s\")) failed: %s",                  fieldname,                 x.getclass().getname()));     }     system.out.println(); }  

... produces:

doublefield comes as: java.lang.double value: 1.230000  textfield comes as: java.lang.string value: 4.56 r.getdouble("textfield") failed - java.lang.classcastexception: java.lang.string cannot cast java.lang.double double.parsedouble(r.getstring("textfield")) succeeded. value: 4.560000 

if unable double.parsedouble() parse string values database either

  • they contain "funny characters" not apparent samples posted, or
  • you're doing wrong.

additional information re: sample file

jackcess returning currentgac string because text field in table:

tabledesign.png

the following java code ...

table t = db.gettable("ntep samples list"); int countnotnull = 0; int countatleast12 = 0; (row r : t) {     string s = r.getstring("currentgac");     if (s != null) {         countnotnull++;         double d = double.parsedouble(s);         if (d >= 12.00) {             countatleast12++;         }     } } system.out.println(string.format(         "scan complete. found %d non-null currentgac values, %d of >= 12.00.",          countnotnull,          countatleast12)); 

... produces ...

scan complete. found 100 non-null currentgac values, 62 of >= 12.00. 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -