java - Calling a method for multiple values in a file -



java - Calling a method for multiple values in a file -

i'm reading in file command line on programme below:

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**

gradebookapp.java

public class gradebookapp { public static void main(string[] args) throws ioexception { 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); } }

i have class containing method "addgrade" takes in parameter "newgradein". parameter grade file beingness read in i.e. a100 or e88. each grade beingness added, i'm replacing original 2d array 1 holds 1 more item.

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; }

i'm blanking on how this. know should utilize for-each loop, i'm not sure how go this.

since want utilize a grade a100 in order store 100 grade pupil a should consider using map instead of 2d array. example:

map<string, double[]> gradetable = new hashmap<>(); public void addgrade(string newgradein) { string row = newgradein.substring(0,1); // utilize 'a' key in hashmap grades of pupil `a` int grade = integer.parseint(newgradein.substring(1)); double[] oldarr = gradetable.get(row); if (oldarr == null) { // when start - hash-map empty need initialize array oldarr = new double[0]; } double[] newarr = arrays.copyof(oldarr, oldarr.length + 1); newarr[newarr.length - 1] = grade; gradetable.put(row, newarr); }

the usage pretty straight forward:

gradebook g = new gradebook(); g.addgrade("a100"); g.addgrade("a95"); g.addgrade("b90"); g.addgrade("b95"); system.out.println(arrays.tostring(g.gradetable.get("a"))); // [100.0, 95.0] system.out.println(arrays.tostring(g.gradetable.get("b"))); // [90.0, 95.0]

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -