csv - Java-errors and inability to locate the answer -
i writing program needs read csv-file. , print out first word on each line ,plus first following number right after it. strangely enough ,my code can work on friends computer not own.
editing nomenclature methods , variabeles english (so easier guys read) has yielded more peculiar errors ,which netbeans cant me locate. short example of how should when works : binnenstad 17.460 bloemekenswijk 8.848 brugse poort - rooigem 17.652 ...
the main class code :
package autobezit; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class autobezit { /** * @param args command line arguments * @throws java.io.filenotfoundexception */ public static void main(string[] args) throws filenotfoundexception { hood brugge = new hood("brugge", 50000); system.out.println(brugge.getinfo()); autobezit situation = new autobezit(); situation.initialise(); } public void initialise() throws filenotfoundexception { scanner sc = new scanner(new file("gent_autobezit.csv")); sc.usedelimiter(";"); (int i=0; i<3; i++){ sc.nextline(); } while(sc.hasnext()){ string name= sc.next(); int number= sc.nextint(); hood hood = new hood(name,number); sc.nextline(); system.out.print(hood.getinfo()); } } }
and 'hoodclass-code'
package autobezit; public class hood { private string name; private int numberofinhabitants; public hood(string name, int numberofinhabitants){ this.name=name; this.numberofinhabitants=numberofinhabitants; } public string getinfo(){ return name+" "+numberofinhabitants; } }
the csv file needs read :
gent in cijfers;;;;;;;;; wijken;;;;;;;;; ;totaal aantal inwoners (2010) [aantal];aantal huishoudens zonder auto (2001) [huishoudens];aantal huishoudens met 1 auto (2001) [huishoudens];aantal huishoudens met 2 auto's (2001) [huishoudens];aantal huishoudens met 3 of meer auto's (2001) [huishoudens];percentage huishoudens zonder auto (2001) [huishoudens];percentage huishoudens met 1 auto (2001) [huishoudens];percentage huishoudens met 2 auto's (2001) [huishoudens];percentage huishoudens met 3 of meer auto's (2001) [huishoudens] binnenstad;17.460;3.347;4.270;730;64;39,8;50,8;8,7;0,8 bloemekenswijk;8.848;1.337;1.707;245;20;40,4;51,6;7,4;0,6 brugse poort - rooigem;17.652;2.602;3.428;484;34;39,7;52,4;7,4;0,5 dampoort;12.030;1.376;2.117;389;30;35,2;54,1;9,9;0,8 drongen;12.946;520;2.571;1.514;170;10,9;53,8;31,7;3,6 elisabethbegijnhof - papegaai;7.086;1.217;1.669;342;24;37,4;51,3;10,5;0,7 gentbrugge;7.407;596;1.681;598;65;20,3;57,2;20,3;2,2 kanaaldorpen en -zone;2.438;353;782;247;27;25,1;55,5;17,5;1,9 ledeberg;9.361;1.351;1.908;262;27;38,1;53,8;7,4;0,8 macharius - heirnis;6.695;973;1.253;233;16;39,3;50,6;9,4;0,6 mariakerke;13.297;858;2.993;1.159;120;16,7;58,3;22,6;2,3 moscou - vogelhoek;4.993;508;1.198;294;27;25,1;59,1;14,5;1,3 muide - meulestede - afrikalaan;6.010;806;1.095;133;18;39,3;53,4;6,5;0,9 nieuw gent - uz;8.137;1.554;1.658;231;32;44,7;47,7;6,6;0,9 oostakker;12.983;691;2.494;1.041;114;15,9;57,5;24;2,6 oud gentbrugge;8.431;914;1.900;378;38;28,3;58,8;11,7;1,2 rabot - blaisantvest;8.254;1.544;1.327;131;9;51,3;44,1;4,4;0,3 sint amandsberg;18.108;1.675;3.956;1.144;99;24,4;57,6;16,6;1,4 sint denijs westrem;5.975;344;1.202;675;97;14,8;51,9;29,1;4,2 sluizeken - tolhuis - ham;10.952;1.575;1.727;209;17;44,6;49;5,9;0,5 stationsbuurt noord;11.538;1.856;3.173;615;48;32,6;55,7;10,8;0,8 stationsbuurt zuid;7.745;875;1.918;494;56;26,2;57,4;14,8;1,7 watersportbaan - ekkergem;7.147;1.637;1.791;244;24;44,3;48,5;6,6;0,6 wondelgem;14.126;989;3.474;966;68;18;63,2;17,6;1,2 zwijnaarde;7.100;419;1.521;678;91;15,5;56,1;25;3,4 totaal;246.719;29.917;52.813;13.436;1.335;30,7;54,2;13,8;1,4 ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;
edit : adding importcommand:
import java.util.locale;
// , sc.uselocale.... right after sc.usedelimiter
sc.uselocale(locale.german);
has solved issues. biggest source of problems has been located :
while(sc.hasnext())
what symptoms on computer? looking @ code , following aspects seem surprising:
- you throw away first 3 lines (sc.nextline()) don't seem checking if file has 3 lines offer. , friends using same csv?
- the csv file contains bad input not accounting it.
i think number format issue , not because file contains floats perhaps because file contains european-formatted numbers (i.e. 17.460 17460) - reason works on friends' computers because using locale , number interpreted "17" instead of "17460".
here initialise function rewritten more robust , locale-specific:
public void initialise() throws filenotfoundexception, parseexception { scanner sc = new scanner(new file("gent_autobezit.csv")); sc.usedelimiter(";"); (int = 0; < 3; i++) { sc.nextline(); } while (sc.hasnext()) { string name = sc.next(); name = name.trim(); if (name.length() == 0) continue; // skip blank lines string number = sc.next(); numberformat nf = numberformat.getnumberinstance(locale.germany); nf.setparseintegeronly(true); number frm = nf.parse(number); hood hood = new hood(name, frm.intvalue()); sc.nextline(); system.out.print(hood.getinfo()); } }
Comments
Post a Comment