1

In the following program I have coded a program that reads in 5 student names, along with marks for each for 5 quizes for each student. I have loades the names in an ArrayList of type String and the quiz marks in an ArrayList of type Double. However I need to load these quiz marks into a Integer and I am not sure how to 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 that needs to become a Integer ArrayList
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 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 the 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 am getting the 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
7
  • so you want integer outputs , but you're getting outputs with decimal places, am i correct ? Commented Jul 15, 2016 at 18:37
  • I just want to read the values in in a Integer ArrayList not a double ArrayList Commented Jul 15, 2016 at 18:38
  • but i still want to output as double Commented Jul 15, 2016 at 18:40
  • please show the expected output format so that your question becomes clear. Commented Jul 15, 2016 at 18:40
  • Why do you want to do that? Commented Jul 15, 2016 at 18:40

3 Answers 3

1

Something like this?

  List<Double> dValues = new ArrayList<>();
  // not shown: add your double values to the dValues list
  List<Integer> iValues =  dValues.stream()
      .map(p -> p.intValue())
      .collect(Collectors.toList());
  // or the equivalent
  //  List<Integer> iValues = dValues.stream()
  //      .map(Double::intValue)
  //      .collect(Collectors.toList()); 

Note: the intValue() method will probably not round like you expect it to, so you might have to get a little fancier with that part of the lambda.

Sign up to request clarification or add additional context in comments.

Comments

1

The Java Math library has a couple of static methods if you want a quick solution, they return doubles, but you can cast them to integers like so

(int)Math.floor(doubleNum); // 4.3 becomes 4
(int)Math.ceil(doubleNum); // 4.3 becomes 5

1 Comment

Note that if you simply want to cast from double to int, (int)someDouble is equivalent to (int)Math.floor(someDouble). Java simply discards the decimal portion of doubles when casting to integers, automatically.
1

In Java if you divide a Integer by a Integer, you get an Integer and result will be loss of precision, Example :-

Integer division    8 / 5 = 1
Double division     8.0 / 5.0 = 1.6
Mixed division  8.0 / 5 = 1.6

So, if you still want to change the inputs to integer, cool!, go ahead just change your inputs to integer.Just make sure one of input is double if you want precision. eg :-

Input  = saumyaraj zala 45 25 14 78 45
Output = Name: saumyaraj zala   Quiz Avg: 41.4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.