Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Stupid Nested For Loop Problem JAVA

  • 04-05-2006 10:59am
    #1
    Closed Accounts Posts: 58 ✭✭


    Hi everybody,

    this was given to me as part of an example in one of my classes. I know what the code is supposed to do, but i dont really know how its done. So basically could someone comment the code and explain how it works.

    It is supposed to sort out 5 "exam" objects given in by the user that are stored in "examArray" and then display the five objects by date. It appears to me that theres only one object being shown on screen in the JOptionPane, but ive been wrong in the past. If this is the case could someone please write a small piece of code doing so.

    Thanks, see code below.
    int lowest_loc = 0;
    
    For (int m = 1; m <examArray.length; m++){
         For (int i = 1; i <examArray.length; i++){
           if(examArray[i] != null){
             if(examArray[i].compareTo(examArray[lowest_loc])){
               Lowest_loc = i;
    
                }
              }
        }
    
    JOptionPane.showMessageDialog(null, examArray[lowest_loc]);
    examArray[lowest_loc] = null; 
    
    
    


Comments

  • Moderators, Politics Moderators Posts: 41,222 Mod ✭✭✭✭Seth Brundle


    Will someone please do my homework?


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I was going to put in joke comments, but after the reading the code. Roffle.


  • Closed Accounts Posts: 58 ✭✭Woof!


    actually its from a sample paper.... dont blame me for not being a fantastic programmer

    http://inismor.ucd.ie/~fintanc/cs1001/COMP10020_sample_answers_2006.pdf


  • Moderators, Politics Moderators Posts: 41,222 Mod ✭✭✭✭Seth Brundle


    Where are your (photocopied?) lecture notes


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Have you tried writing out exactly what each line is trying to do yet?


  • Advertisement
  • Closed Accounts Posts: 58 ✭✭Woof!


    Where are your (photocopied?) lecture notes

    Theyre all on that site but there's only the tiniest little piece on nested loops in there and it involves triangles which i dont find helpful at all.

    So these are the things i understand/need help with:

    ok i know everytime the loop with "m" in it iterates the loop with "i" in it iterates 5 times.
    (i understand what it does i just dont know how and why)

    if the examArray has a value then compare it to lowest_loc

    then lowest_loc is set to be i (this index)

    but only lowest_loc is getting returned in the JOptionPane so how is that all of the objects being returned in order?

    Thanks for your help and sorry for not making it clear enough the first post that this wasnt homework!:confused:


  • Closed Accounts Posts: 58 ✭✭Woof!


    also if im comparing the two indices to try and get the first one should it not specify somewhere along the way: if i < lowest_loc?


  • Closed Accounts Posts: 888 ✭✭✭themole


    the reason its not making sense is because you left out a } at the end, after the last line.

    so the JOptionpane is in the outer for loop.

    so, the code actually makes sense then.

    what happens is that at each iteration of the outer loop the lowest value is found printed and removed from the array.

    thats how they get printed in sorted order.

    Looking at the link i see that this is fintan costellos introduction to java course, which I am actually a demonstrator for :)


  • Closed Accounts Posts: 1 grover06


    Can you tell me more about loops?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    themole wrote:
    Looking at the link i see that this is fintan costellos introduction to java course, which I am actually a demonstrator for :)
    So we'll leave you to handle this one then? :P


  • Advertisement
  • Closed Accounts Posts: 58 ✭✭Woof!


    that's great..... cheers and also i didnt leave out the }... that was fintan!

    also whats your name and what do u look like? i prob know u to see anyway


  • Moderators, Politics Moderators Posts: 41,222 Mod ✭✭✭✭Seth Brundle


    Woof! wrote:
    that's great..... cheers and also i didnt leave out the }... that was fintan!

    Glancing at the linked exam I can see the closing brace!
    java.gif


  • Closed Accounts Posts: 58 ✭✭Woof!


    crap..... erm... typo??

    cheers for the help lads, i now have to do the c sample papers!


  • Closed Accounts Posts: 888 ✭✭✭themole


    Woof! wrote:
    that's great..... cheers and also i didnt leave out the }... that was fintan!

    also whats your name and what do u look like? i prob know u to see anyway

    My home page is: www.liammcdermott.com/

    and for my sins I have a bebo account :(
    http://theirishmole.bebo.com


Advertisement