Results 1 to 3 of 3
  1. #1
    DavidPynes is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Can someone clarify this piece of code for me.

    The program creates a multidimensional array and then a Max number in a user specified column.

    I don't understand how the if lines actually work in this code. Please explain your thoughts.

    public static void main(String[] args)
    {

    double [][] familyCellBills = { {45.24, 54.67, 32.55, 25.61},
    {65.29, 49.75, 32.08, 26.11},
    {75.24, 54.53, 34.55, 2816} };

    String [] familyMembers = {"Joe", "Jane", "Mike" , "Sarah"};
    for ( int i = 0; i < familyMembers.length; i++ )
    System.out.println( "Family member" + i + "; " + familyMembers[i] );

    Scanner scan = new Scanner( System.in );
    int currentMember;
    do
    {
    System.out.print( "Enter a member number between 0 and3 > ");
    currentMonth = scan.nextInt();
    }
    while (currentMonth < 0 || currentMonth > 3 );

    double memberMaxBill = FamilyCellBills[0][currentMember];
    for ( int i = 1; i < familyCellBills.length; i++ )
    {
    if ( currentMember < familyCellBills[i].length )
    //UPDATEMAXBILL
    if (familyCellBills[i][currentMember] > memberMaxBill )
    memberMaxBill = familyCellBills[i][currentMember];
    }
    }
    }

    NumberFormat priceFormat = NumberFormat.getCurrencyInstance();
    System.out.println( "\nThe total family cell bills during "
    + months[currentmember] + " is "
    + priceFormat.format(memberMaxBill));

    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    does this code even work?

    Java Code:
    while (currentMonth < 0 || currentMonth > 3 );
    since you have 3 months bills only i'm guessing you mean this to say:
    Java Code:
    while (currentMonth > 0 && currentMonth <= 3)

    Java Code:
    double memberMaxBill = FamilyCellBills[0][currentMember];
    where is FamilyCellBills with capital 'F' defined? Java is case sensitive.

    How the IF works:
    maxMemberBill is set the the first person in the array i.e. familyCellBills[0] who is Joe.
    then you have this conditional statement in your while loop:
    Java Code:
    if ( currentMember < familyCellBills[i].length )
    which means that if current member index (index 0) is less than number of members (4) then carry on (so long as currentMember is incremented after each loop then this will cause the code in the IF statement to run only four times - and then your While loop should be broken by adding another IF statement after this one to check if currentMember >= 4)

    your next IF statement checks Joe's bill is greater than the other members bills. if any other bill is greater, maxMemberBill is set to the greater one.
    Java Code:
    memberMaxBill = familyCellBills[i][currentMember];

    your code is messy and could be improved still
    Last edited by ozzyman; 04-17-2011 at 10:08 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DavidPynes View Post
    The program creates a multidimensional array and then a Max number in a user specified column.

    I don't understand how the if lines actually work in this code. Please explain your thoughts.
    This isn't your code, right? Go ask the author of the code if you don't understand it; s/he knows best.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 10:14 AM
  2. problem getting a piece of data out of XML
    By Sparky in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 09:30 PM
  3. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  4. small piece of code: cannot set a max
    By senca in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 08:26 PM
  5. Decode this piece of Code
    By mikeyl62 in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 08:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •