java - Finding smallest and largest population and the average -
i've been working on getting program work. i'm having little trouble getting program read files have created, census2000 , census2010. these contain 50 states , population in 2000 , 2010. believe rest of program correct. told use methods find smallest population, largest population , average. here 2 lines 2000 file:
alabama 4447100
alaska 626932
here program:
public static void main(string[] args) throws ioexception { string state = ""; int population = 0; int p = 0, s = 0, pop = 0, stat = 0, populate = 0, sum = 0; file f = new file("census2000.txt"); scanner infile = new scanner(f); infile.usedelimiter("[\t|,|\n|\r]+"); while (infile.hasnext()) { checksmall(p, s); checklargest(pop, stat); checkaverage(populate, sum); population = infile.nextint(); state = infile.next("/t"); system.out.println(state + "has" + population + "people"); } system.out.println(state + "has smallest population of" + population); prw.close(); } public static boolean checksmall(int p, int s) { boolean returnvalue; if (p < s) { returnvalue = true; } else { returnvalue = false; } return (returnvalue); } public static boolean checklargest(int pop, int stat) { boolean returnval; if (pop > stat) { returnval = true; } else { returnval = false; } return (returnval); } public static int checkaverage(int populate, int sum) { int retval; retval = populate + sum; return (retval); } } what doing wrong?
i believe problem here:
state = infile.next("/t"); i think you're trying skip tab in file , read state? reading in line , splitting line using \t delimiter.
string line; while (infile.hasnextline()){ line = infile.nextline(); string data[] = line.split("\\s+"); state = data[0]; population = integer.parseint(data[1]); } edit: other answer points out, you're attempting perform functions on file's data before it's read.
Comments
Post a Comment