This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing from Double ArrayList to Integer ArrayList  RSS feed

 
Jeremy Bon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



For the input:

   

I am getting the correct output

   
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Bon wrote:. . . I have loades the names in an ArrayList of type String and the quiz marks in an ArrayList of type Double.  . . .
That sounds a very bad idea to me. You sho‍uld create a Student class which encapsulates the name and mark, and put that into your List.
List<Student> studentsList = new ArrayList<>();
The <> was introduced in Java7 and it means to use same type as before.
Don't use a Scanner to read Strings and convert them to doubles later. Use the Scanner to read the doubles directly. If you have multiple marks per student, that is a bit more difficult. It is rather awkward, but you can write a constructor like this:-
public Student(String name, double mark1, double mark2,
        double mark3, double mark4, double mark5) ...
Alternatively you can create an array of marks.

Don't have a method which calculates the average like that; if the Student objcet has the marks, the Student object shou‍ld have the method to calculate the average.

What do you mean by changing doubles to ints? So you want to round with a method like this one? Or do you simply want a cast, which rounds towards 0? Each is easy enough to do. You can do it in the Student#average method, or have two average methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!