Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Nested For...each Loop?

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Nested For...each Loop?

    Hi, I have 3 classes in My Program. One is the Main class, another class for students and another class for the subjects and their marks, which are store in arrayLists....

    When the student enters his marks an instance of student is created. Now, the student can add more than one subject, thus making student have more than one instance of subject. I need to add the ArrayLists of homeworkMarks in more than one instance subject so that I can work out the average of the 2 subjects together.

    Any Idea howe this can be done?

    Below is my code... I was thinking of using a nested For each loop, but I'm not 100% sure how to use it :-(

    public double homeworkAverage() {
          double homeworkAddition = 0;
     
          for (Subject s : <no idea what to put here>) {
              homeworkAddition = homeworkAddition + homeworkMark.get(i);
          }

    Regards


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested For...each Loop?

    The for-each loop acts on a collection. So to use a for-each loop that iterates over instances of Student, you need a collection of Students.

    List<Student> students = new ArrayList<Student>();
    for(Student s : students){
       s.someMethod();
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested For...each Loop?

    At this point I have one instance of "student".. If the student adds more than one subject, I need to add the homeworkMark of both instances of "student"... Lets say I have

    A student who decides to add marks for 2 subjects... thus I have 2 different arrayLists in 2 different instances of subject which I need to add...

    You get me?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested For...each Loop?

    Ah sorry, I wrote "Student" when I meant "Subject". My example should look like this:

    List<Subject> subjects= new ArrayList<Subject>();
    for(Subjects : subjects){
       s.someMethod();
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested For...each Loop?

    still cannot get it... this is my full class...
    import java.util.ArrayList;
    public class Subject
    {
       int subjectNumber;
       ArrayList<Double> homeworkMark;
       ArrayList<Double> examinationMark;
       public Subject(int subjectNumber) {
          this.subjectNumber = subjectNumber;  
          this.homeworkMark  = new ArrayList<Double>();
          this.examinationMark = new ArrayList<Double>();
       }
     
       public int getSubjectNumber() {
           return subjectNumber; 
        }
     
       void setSubjectNumber (int subNum) {
            subjectNumber= subNum;
       }
     
     
       public double homeworkAverage() {
          double homeworkAddition = 0;
            for(double s : homeworkMark) {
                  homeworkAddition = homeworkAddition + s;   
                }
          //works out the average of the homewrok marks entered
          double homeworkAverage = homeworkAddition/homeworkMark.size();
          return homeworkAverage;
        }
     
       public double examinationAverage() {
          //Works out the addition of the arrayList examinationMark
          double examinationAddition = 0;
          for (int i = 0; i <= examinationMark.size()-1; i++) {
              examinationAddition = examinationAddition + examinationMark.get(i);
          }
          //works out the average of the homework marks entered
          double examinationAverage = examinationAddition/examinationMark.size();
     
          return examinationAverage;
       }
     
       public double overallMark() {
           double overallMark = ((20.0/100.0) * homeworkAverage()) + ((80.0/100.0) * examinationAverage());
           return overallMark;
       }
     
       public char gradeChecker(){
           char grade ='F';
                  if(overallMark() >= 75) {
                    grade = 'A'; 
                  }
                  else if (overallMark() >= 60 && overallMark() < 75){
                    grade = 'B'; 
                  }
                  else if (overallMark() >= 50 &&  overallMark() <= 60){
                    grade = 'C'; 
                  }
                  else if (overallMark() >= 40 &&  overallMark() <= 50){
                    grade = 'D'; 
                  }
                  else if (overallMark() >= 35 &&  overallMark() <= 40){
                    grade = 'E'; 
                  }
                  else if (overallMark() < 35 ){
                    grade = 'F'; 
                  }
     
            return grade;
       }
     
       public void addToHomeworkMark(double homeMark) {
           homeworkMark.add(homeMark);
       }
     
       public void addToExaminationMark(double examMark) {
           examinationMark.add(examMark);
       }
    }

    I need this to work for both homeworkMark and examinationMark. Att the moment its only calculating of the last subject entered and not of the first and the second together.

    I'm totally lost :/

    regards, and thanks in advance

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested For...each Loop?

    I'm not totally sure what your question is. What does the code you posted do? What does it do wrong?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested For...each Loop?

    The user must... enter name, surname student number.... thats one class...

    then it asks him to enter marks for hw and exams for one subject, so thats an instance of subject...in array sub...

    it asks if he wants to enter again, if yes, a new instance of subject is created and put into array sub, again with the arraylist of hw marks and exam marks...

    I must add arraylist of HomeworkMark of the first subject he added, with the arraylist of homework marks of the second subject he entered. These are both in arraylist sub... in the main method...

    Mind-f*ck in my op

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested For...each Loop?

    You've described what you want the code to do, and you've posted some code, but which part of your goals are you confused about? Which specific part are you stuck on? Where specifically in the code are you trying to implement that goal? What does that part of the code do instead of the goal?

    It's very hard to answer general "how do I do this" type questions other than by pointing you to the basic tutorials. However, if you have a more specific question like "I tried to do X, expected Y, but Z happened instead", that's much easier to work through with you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested For...each Loop?

    I need that it works out the addition of both arrayLists of the subject together. SO i can work out an overall average.

    Example: I'm malcolm, and I have 2 subjects... English and Maltese. I need to add the marks of both of them(both instances of subject) so that ill be able to work an average.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested For...each Loop?

    We're going in circles now, but the question remains: what about those requirements are you stuck on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested For...each Loop?

    Quote Originally Posted by KevinWorkman View Post
    We're going in circles now, but the question remains: what about those requirements are you stuck on?
    Yes we are, haha.

    Ok, so basically...If The user enters the the marks for the first subject.... (example: 20, 30 and 40)
    The enters the marks for the second subject ( example 50, 60 and 70);

    The program works out the addition of subject 1 alone (20 + 30 + 40) and of subject 2 alone (50 + 60 + 70)

    I require to add both arrayLists of the subjects in both instances of subject to get the addtion of ( 20 +30 + 40 +50 +60 +70)

    Thats where I am stuck. How can i access both homeworkMark arrayLists from my 2 different instances of subject?? If you require me to send you the file, I'll send it as a link in a private message.

  12. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Nested For...each Loop?

    pseudo code:
    marksadded = 0
    counter=0
    subjects = student.getSubjects();
    loop1 over subjects
       marks = subject.getMarks();  
       loop2 over marks
          counter++
          marksadded += mark
       end loop2
    end loop1
    average = marksadded/counter

  13. The Following User Says Thank You to PhHein For This Useful Post:

    KevinWorkman (January 29th, 2014)

Similar Threads

  1. nested while loop
    By Amruta N in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 12:35 AM
  2. Need help with this nested loop
    By beerye28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 09:51 PM
  3. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  4. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM