java - Read from array and calculate average -
java - Read from array and calculate average -
i trying write code reads row of characters array, assign characters integer , average integers of row, every row in array. here have far :
scanner in = new scanner(new file("grades.txt")); while(in.hasnextline()) { int gp = 0; double gpa = 0; string line = in.nextline(); if(line.length() == 0) { continue; } line = line.substring(line.length()-15, line.length()); string[] letters = line.split(" "); (int = 0; < letters.length; i++) { if (letters[i].equals("h")) { gp += 7; } else if (letters[i].equals("d")) { gp += 6; } else if (letters[i].equals("c")) { gp += 5; } else if (letters[i].equals("p")) { gp += 4; } else if (letters[i].equals("f")) { gp += 0; } } gpa = gp / letters.length; system.out.println(gpa); }
here array working :
[, d, d, c, p, h, h, c] [, h, h, h, d, d, h, h] [, c, f, c, d, c, c, c] [, f, f, f, p, c, c, c] [, f, c, f, c, d, p, d] [, d, d, f, d, d, f, d] [, p, f, p, f, p, f, p]
here current output of code :
5.0 6.0 3.0 2.0 3.0 4.0 2.0
contents of text file :
45721 chris jones d d c p h h c d 87946 jack aaron h h h d d h h h 43285 ben adams c f c d c c c p 24679 chuck doherty f f f p c c c f 57652 dean betts f c f c d p d h 67842 britney dowling d d f d d f d d 22548 kennedy blake p f p f p f p p
am on right path? there easier method? help appreciated.
just quick note save grief later
shouldn't
line = line.substring(line.length()-16, line.length()-1);
be instead
line = line.substring(line.length()-15, line.length());
to grades?
and you'll need set if statements loop, loops on each value of array (replace letters[1]
letters[i]
)
java arrays
Comments
Post a Comment