java - Hyphen separated in between column in a CSV File -
i have csv file, contents this:
country,player,runs,scorerate,matchdate,weekday,ground,versus,url afghanistan,mohammad shahzad,118,97.52,16-02-2010,tue,sharjah ca stadium,canada,../matches/matchscorecard_odi.asp?matchcode=3087 afghanistan,mohammad shahzad,110,99.09,01-09-2009,tue,vra ground,netherlands,../matches/matchscorecard_odi.asp?matchcode=3008 afghanistan,mohammad shahzad,100,138.88,16-08-2010,mon,cambusdoon new ground,scotland,../matches/matchscorecard_odi.asp?matchcode=3164 afghanistan,mohammad shahzad,82,75.92,10-07-2010,sat,hazelaarweg,netherlands,../matches/matchscorecard_odi.asp?matchcode=3153
i have find total scores afganisthan player in year 2010.
i wrote code, shows , exception , outputs
java.lang.numberformatexception: input string: "" @ java.lang.numberformatexception.forinputstring(unknown source) @ java.lang.integer.parseint(unknown source) @ java.lang.integer.parseint(unknown source) @ a.main(a.java:38) 35135
however, output should 28 something.
here code.
import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; public class { public static void main(string args[]) throws filenotfoundexception { string csv="c:\\users\\dipayan\\desktop\\odi-batting.csv"; bufferedreader br=new bufferedreader(new filereader(csv)); string line=" "; int sum=0; int count=0; int []a=new int[10000]; try { br.readline(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { while((line=br.readline())!=null) { string [] f= line.split(","); if((f[4]="2010") != null) { a[count]=integer.parseint(f[2]); sum=sum+a[count]; count++; } } } catch (numberformatexception | ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println(sum); } }
there few problems here. first, neglected split date , check year. second, since these strings, should compare them equals
method. so, contents of while
loop should more or less this:
string country = f[0]; string date = f[4]; string year = date.split("-")[2]; if (country.equals("afghanistan") && year.equals("2010")) { a[count] = integer.parseint(f[2]); sum += a[count]; count++; }
Comments
Post a Comment