java - changing from Double ArrayList to Integer ArrayList -
in following program have coded program reads in 5 student names, along marks each 5 quizes each student. have loades names in arraylist of type string , quiz marks in arraylist of type double. need load these quiz marks integer , not sure how change this
import java.util.scanner; import java.util.arraylist; public class onemoretime { public static final double max_score = 15 ; public static final int namelimit = 5; public static void main(string[] args) { arraylist<string> names = new arraylist<>(); arraylist<double> averages = new arraylist<>(); //line needs become integer arraylist scanner in = new scanner(system.in); (int = 0; < namelimit; i++) { string line = in.nextline(); string[] words = line.split(" "); string name = words[0] + " " + words[1]; double average = findaverage(words[2], words[3], words[4], words[5], words[6]); system.out.println("name: " + name + " quiz avg: " + average); names.add(name); averages.add(average); } } public static double findaverage(string a, string b, string c, string d, string e) { double sum = double.parsedouble(a) + double.parsedouble(b) + double.parsedouble(c) + double.parsedouble(d) + double.parsedouble(e); return (sum / namelimit); } }
for input:
sally mae 90 80 45 60 75 charlotte tea 60 75 80 90 70 oliver cats 55 65 76 90 80 milo peet 90 95 85 75 80 gavin brown 45 65 75 55 80
i getting correct output
name: sally mae quiz avg: 70.0 name: charlotte tea quiz avg: 75.0 name: oliver cats quiz avg: 73.2 name: milo peet quiz avg: 85.0 name: gavin brown quiz avg: 64.0
something this?
list<double> dvalues = new arraylist<>(); // not shown: add double values dvalues list list<integer> ivalues = dvalues.stream() .map(p -> p.intvalue()) .collect(collectors.tolist()); // or equivalent // list<integer> ivalues = dvalues.stream() // .map(double::intvalue) // .collect(collectors.tolist());
note: intvalue() method not round expect to, might have little fancier part of lambda.
Comments
Post a Comment