Yes you are right. Using <=4 or <5 works as desired.

Craig

On 1/11/10, Nic Fox <[email protected]> wrote:
> In this case you need to use < 5 rather than <=5 in the for-loop as
> Tom has shown. Remember that a count starting from 0 is the first
> iteration, so a count up to and including 5 is actually 6 iterations.
>
> The same logic applies to array indexes which also start with 0, but
> note that the length property of an array will be the largest array
> index + 1 which is why we use i < myArray.length when traversing a
> whole array.
>
> Regards
> Nic
>
> On 12/01/2010, at 5:16 AM, Thomas Schiefer wrote:
>
>> An other possibility is:
>>    ....
>>    ArrayList<PersonData> persons = new ArrayList<PersonData>();
>>       for(int i = 0; i <= 5; i++) {
>>
>>       Scanner input = new Scanner ( System.in );
>>       System.out.println();
>>       System.out.print( "Please enter the persons name (First Last)
>> or 'stop' to end program: " ); // prompt
>>       String perName = input.nextLine();
>>
>>       if ( perName.equals("stop") ) { //use equals instead of
>> compareTo here, then you don't have to compare two int values to get
>> a boolean
>>
>>          System.out.println( "Program ended." );
>> //       System.exit(0); //use this to exit the appliaction
>>          return; //use this to return from a method. in this case
>> you are in the main method and exit the application
>>       }
>> ...
>>
>> If you do it with a while or with a for-loop does not matter. You
>> can always achieve the same behaviour with both variants.
>>
>> Regards
>> Tom
>>
>> On Mon, Jan 11, 2010 at 6:59 PM, Dainis Brjuhoveckis
>> <[email protected]
>> > wrote:
>> I would recommend using "while (persons.size() <= 5 )" instead of
>> "while (persons.size() < 6" ), because this way it is easier to see
>> that 5 (not 6) persons are required.
>>
>>
>> On Mon, Jan 11, 2010 at 7:48 PM, Craig Jensen <[email protected]
>> > wrote:
>> Perfect!
>>
>> Thank you! I am still learning the available options to append to an
>> array (or otherwise) within Java (obviously) 'persons.size', duh!
>>
>> It works great.
>>
>> Craig
>>
>> On Mon, Jan 11, 2010 at 10:37 AM, Cecil Haertel III <[email protected]
>> > wrote:
>> check out the added code and edited code below.
>>
>> Now you should move some of the other things out of that loop if
>> this is way you would like to do it.
>> There is no need to recreate some of those objects during your
>> iteration.
>>
>> I think this is what you are talking about.
>>
>> On Mon, Jan 11, 2010 at 12:17 PM, Craig Jensen <[email protected]
>> > wrote:
>> Ok, let me be more specific. The loop iterations must be five only
>> and end without a sentinel even though right now I have included a
>> sentinel. Plus, I cannot use a break.
>>
>> So I was not able to see a syntax that would accomplish this (that's
>> why I threw in the sentinel)
>>
>> Synopsis of the app (I have also included the source of the class
>> with the loop):
>>
>> * prompt for each of 7 different inputs
>> * once they are input, display the results including a product of
>> two of the inputs
>> * return and begin a new prompt all over.
>>
>>  public class AddBook // Modified to include requirement of positive
>> num entry for donations (amount of and number of)
>>  {
>>     // main method begins execution of java application
>>
>>     public static void main( String args[] )
>>     {
>>       ArrayList persons = new ArrayList(); // Array to handle
>> multiple entries of persons
>>    boolean stop = false; // This flag will control whether we exit
>> the loop below
>>    // Loop until user types "stop" as the person's name:
>>    while (persons.size() < 6 )
>>
>>        {
>>        // create scanner to obtain input from command window
>>        Scanner input = new Scanner ( System.in );
>>        System.out.println();  // a blank line
>>        System.out.print( "Please enter the persons name (First Last)
>> or 'stop' to end program: " ); // prompt
>>        String perName = input.nextLine(); // read persons name
>> /*
>>    if ( perName.compareTo("stop") == 0) // Check whether user
>> indicated to stop program
>>        {
>>        System.out.println( "Program ended." );
>>        stop = true;
>>        }
>>    else
>>        {
>> */
>>        // User did not indicate to stop, so continue reading info
>>        PersonData person;
>>        float donationNum; // first number, number of donations for
>> the year
>>        float donationAmount; // second number, amount per donation
>> (yeah I know it doesn't allow for different amounts).
>>        String Address;
>>        String City;
>>        String State;
>>
>>     System.out.print ( "Please enter the person's Address: " );
>>     Address = input.nextLine(); // Address validation and exception
>> catch needed
>>
>>     System.out.print ( "Please enter the person's City: " );
>>     City = input.nextLine(); // City validation needed
>>
>>     System.out.print ( "Please enter the person's State: " );
>>     State = input.nextLine(); // Validation for State needed
>>
>>     String zip = "12345";
>>
>>     //String zipCodePattern = "\\d{5}";
>>     System.out.print ( "Please enter the person's Zip in five number
>> format (88388): " );
>>     zip = input.nextLine();
>>     //while (zip != zipCodePattern) // my attempt at a zip
>> validation, gave me an infinite loop, will try again later
>>     //    {
>>     //    System.out.print( "The Zip code must be entered in the
>> correct format. " +
>>     //            "Please enter the Zip Code again: "); // Ask for
>> correct Zip entry
>>     //    System.out.println(zip.
>> matches(zipCodePattern));
>>     //    }
>>
>>     System.out.print( "Please enter number of donations: " ); //
>> prompt for donation number
>>     donationNum = input.nextFloat();
>>     while (donationNum <= 0) // validate for positive number
>>         {
>>         System.out.print( "The number of donations must be a
>> positive value. " +
>>                 "Please enter the number of donations again: " ); //
>> prompt for positive value for donations
>>         donationNum = input.nextFloat();
>>         }
>>     System.out.print( "Please enter donation amount: $" ); // prompt
>> for donation amount
>>     donationAmount = input.nextFloat();
>>     while (donationAmount <= 0) // validate for positive amount
>>         {
>>         System.out.print( "The donation amount must be a positive
>> value. " +
>>                 "Please enter the donation amount again: " ); //
>> Prompt again after invalid entry
>>         donationAmount = input.nextFloat();
>>         }
>>
>>     person = new PersonData(perName, donationNum, donationAmount,
>> Address, City, State, zip); // fields included in our person entries
>>         //yearlyDonations = donationNum * donationAmount; // multiply
>>         System.out.print( person.getName() ); // display person's name
>>         System.out.println(); // my way (until I learn better) of
>> formatting the output to single lines from here down ->
>>         System.out.print( person.getAddress() );
>>         System.out.println();
>>         System.out.print( person.getCity() );
>>         System.out.println();
>>         System.out.print( person.getState() );
>>         System.out.printf( ", "); // prob a better way to get a
>> comma between the state and zip... I'm learning
>>         System.out.print( person.getZip() );
>>         System.out.println();
>>         System.out.print( person.getName() );
>>         System.out.printf( "'s yearly donations is: $%,.2f\n",
>> person.getYearlyDonations() );  // display yearly donations
>>         persons.add(person);
>>         }
>> */
>>        }
>>    } // end method main
>> } // end class AddBook
>>
>>
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>>
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>>
>>
>>
>> --
>> Thomas Schiefer
>> Mobile: +43 650/7020481
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
>

-- 
Sent from my mobile device
-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to