• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Changing from Double ArrayList to Integer ArrayList

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

   
 
Marshal
Posts: 80271
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic