java - Input missmatch exception -
so far have text file looks this:
// comment, lines start // // (and blank lines) should ignored [electrictool data] // data rechargeable, power, timesborrowed, onloan, toolname, itemcode, cost, weight true,18v,12,false,makita bhp452rfwx,rd2001,14995,1800 etc...
and following code go through text file:
public void readdata() { try{ filedialog filedialogbox = new filedialog(myframe, "open", filedialog.load); filedialogbox.setvisible(true); string filename = filedialogbox.getfile(); file datafile = new file(filename); scanner scanner = new scanner(datafile); string lineoftext; string typeofdata=""; while(scanner.hasnext()) { lineoftext = scanner.nextline().trim(); electrictool electrictool = new electrictool(); if (lineoftext.startswith("[electrictool data]")){ typeofdata="electrictool"; } else if (!lineoftext.isempty() ){ if (!lineoftext.startswith("//")){ if ( typeofdata.equals("electrictool")){ scanner sc = new scanner(lineoftext).usedelimiter(","); electrictool.extracttokens(sc); toollist.add(electrictool); itemcount++; sc.close(); } } } } scanner.close(); } catch(filenotfoundexception ex) { system.out.println("error: file not found! "); } }
and problematic method:
public void extracttokens(scanner sc) { recheargable = sc.nextboolean(); power = sc.next().trim(); super.extracttokens(sc); }
the recheargable declared boolean: private boolean recheargable;
it gives me missmatch exception when gets rechargeable=sc.nextboolean(); maybe problem in if statements in readdata method ? have tried redo if statements, did not worked. ideas how fix missmatch ?
i wanted verify , created following test:
public static void main(string[] args) { scanner sc = new scanner("true,18v,12,false,makita bhp452rfwx,rd2001,14995,1800").usedelimiter(","); if (sc.hasnext()) { try { system.out.println(sc.nextboolean()); } catch (exception e) { system.err.println(sc.next()); } } while (sc.hasnext()) { system.out.println(sc.next()); } }
its working should, change code following determe problem
public void extracttokens(scanner sc) { try { recheargable = sc.nextboolean(); } catch (exception e) { system.err.println("troublemaker: " + sc.next()); } power = sc.next().trim(); super.extracttokens(sc); }
edit #1: according answer, can't confirm, may want try more error detection. guess problem @ point.
public void extracttokensx(scanner sc) { try { recheargable = sc.nextboolean(); power = sc.next().trim(); super.extracttokens(sc); } catch (exception e) { system.err.println("dumping rest of troubeling line: "); while (sc.hasnext()) { system.err.print(sc.next() + " - "); } } }
Comments
Post a Comment