Sunday 15 August 2010

java - Reading in from a file and adding to 2D array -



java - Reading in from a file and adding to 2D array -

i have 2 different programs, 1 contains method "addgrade" designed add together new grade 2d array (gradetable). 1 array of 2d array category each grade should in, , sec element grades each category. here program:

public class gradebook { private string name; private char[] categorycodes; private string[] categories; private double[] categoryweights; private double[][] gradetable; public gradebook(string namein, char[] categorycodesin, string[] categoriesin, double[] categoryweightsin) { name = namein; categorycodes = categorycodesin; categories = categoriesin; categoryweights = categoryweightsin; gradetable = new double[5][0]; } public boolean addgrade(string newgradein) { char row = newgradein.charat(0); int grade = integer.parseint(newgradein.substring(1)); double[] oldarr = gradetable[row]; double[] newarr = arrays.copyof(oldarr, oldarr.length + 1); newarr[newarr.length - 1] = grade; gradetable[row] = newarr; homecoming row != 0; }

the sec programme reads in file command argument. bolded text represents grades beingness read in. letter stands category each grade should in, , number actual grade. file is

class="lang-none prettyprint-override">student1 5 activities 0.05 q quizzes 0.10 p projects 0.25 e exams 0.30 f final 0.30 **a100 a95 a100 a100 a100 q90 q80 q100 q80 q80 r90 p100 p95 p100 p85 p100 e77.5 e88 f92**

in sec program, i'm trying loop through each grade in file , phone call addgrade method on added 2d array. i'm unsure of how phone call method each individual grade. also, i'm pretty sure addgrade method isn't right. help appreciated. sec program:

public class gradebookapp { string filename = ""; string name = ""; char[] categorycodes = new char[5]; string[] categories = new string[5]; double[] categoryweights = new double[5]; double[][] gradetable; if (args.length > 0) { (int = 0; < args.length; i++) { system.out.println("reading file \"" + args[i] + "\"." + "\n\tcreating gradebook object." + "\n\tadding grades gradebook object." + "\nprocessing of file complete."); filename = args[i]; scanner scanfile = new scanner(new file(filename)); name = scanfile.nextline(); int catcodes = integer.parseint(scanfile.nextline()); (i = 0; < catcodes; i++) { string[] = scanfile.nextline().split(" "); if(all.length == 3 && all[0].length() == 1 && all[2].matches("(\\d+\\.\\d+)")){ categorycodes[i] = all[0].charat(0); categories[i] = all[1]; categoryweights[i] = double.parsedouble(all[2]); } } gradebook mygb = new gradebook (name, categorycodes, categories, categoryweights);

you'd improve off having each list of grades arraylist<double> rather double[]. it's hard work on jvm (and on programmer!) having re-create whole array each time can increment length , add together new one. if utilize arraylist<double> gradelist, can just

gradelist.add(grade);

without needing copying.

i consider having larger construction map rather array. rather having two-dimensional array, have hashmap<character,list<double>> maps row onto list of grades row. avoids having convert between characters , doubles, you're (implicitly) doing.

finally, addgrade() method ought take char , double (a row , new grade), rather string: you're making lot of work having process inappropriate info structures.

once you've done this, calling addgrade each item should easy. 1 time you've extracted string representing particular grade (say, string gr = "e77.5") can add together list within hashmap grademap this:

char row = gr.charat(0); double grade = double.parsedouble(gr.substring(1)); grademap.get(row).add(grade);

i think you'll need supply more info if need more help that.

java arrays

No comments:

Post a Comment