Looks like you have misunderstood the intent of placing all the
Identification details in an object. This means that each array index
should point to a unique instance of an Identification object. Your
code appears to treat each field inside the object as a separate
position in the array which just seems to be a really odd way of
programming that sort of thing.
It would be rare to use explicit array indexes in most code. Usually
I'd use a counter in a for-loop or set up a counter in a while loop
and then access each instance of an object in the array that way, e.g.
Identification[] ids = new Identification[5];
for(int i = 0; i < ids.length; i++) {
ids[i] = new Identification();
ids[i].fname = input.next();
ids[i].lname = ...
...
}
Obviously the code above is incomplete—you could use it as the basis
for rewriting your program's input routine. Hope this helps.
On 26/01/2010, at 5:40 PM, Craig Jensen wrote:
Array built, new problem:
I know the for loop at the bottom is not correct and I can fix that
I believe. What I am now not able to understand is whether I needed
to explicitly assign variable placeholders for the array index
ids[0] and so one before the scanner lines? This is the location of
the current error:
-----------error---------------------------
run:
Please enter a person's First Name: craig
Exception in thread "main" java.lang.NullPointerException
at AddBook4.main(AddBook4.java:97)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
-----------/error---------------------------
new code-
import java.util.*; // program uses class Scanner
// Class creation to setup place holders for the required data
elements for retrieval in my Public
// class later
class Identification {
public String
fname, lname, address, city, state;
public int zip;
public float donationNum, donationAmount;
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getZip() { // String rather than float, I guess, since
US zips can contain '-'?
return zip;
}
public float getYearlyDonations() {
return donationNum * donationAmount;
}
} // end of Identification class
public class AddBook4 // 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[] )
{
Identification[] ids = new Identification[50];
//ArrayList persons = new ArrayList(); // Array to handle
multiple entries of persons
// while ( persons.size() <5 ) // limit to 5 iterations
// while ( ids.length <5 )
//{
// 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 a person's First Name:
" ); // First prompt. Get First Name
ids[0].fname = input.nextLine();
//ids. = input.nextLine(); // read persons First name -
initialize Fname variable
System.out.print( "Please enter a person's Last Name: " ); //
Get Last Name
ids[1].lname = input.next();
// Section requesting user input-------------ends @ line 157
System.out.print ( "Please enter the person's Address: " );
ids[2].address = input.next(); // Address validation and
exception catch needed
System.out.print ( "Please enter the person's City: " );
ids[3].city = input.next(); // City validation needed
System.out.print ( "Please enter the person's State: " );
ids[4].state = input.nextLine(); // Validation for State needed
System.out.print ( "Zip Code in five number format please: " );
ids[5].zip = input.nextInt(); // Get zip - and validate
// Input request for number of donations. Includes validation
for a positive integer.
System.out.print( "Please enter number of donations: " ); //
prompt for donation number
ids[6].donationNum = input.nextFloat();
while (ids[6].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
ids[6].donationNum = input.nextFloat(); // prompt for
another try for valid input
}
// Input reuest that includes validation for positive integer.
Validation for a numeral needed later
System.out.print( "Please enter amount per donation: $" ); //
prompt for donation amount
ids[7].donationAmount = input.nextFloat();
while (ids[7].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
ids[7].donationAmount = input.nextFloat(); // prompt for
another try for valid input
}
// End User Data Request section
// display User Input data
// // Output of our data gathered from above. Blank lines and
comma included for readability of output.
//yearlyDonations = donationNum * donationAmount; // multiply
System.out.printf("\n"); // Print a blank line between user
entry and results for readability
System.out.print( ids[0].getFname() ); // display person's
First Name
System.out.printf(" "); // Get the First and Last names on
same line with a space
System.out.print( ids[1].getLname() ); //display person's
Last Name
System.out.println(); // Print Lname
System.out.print( ids[2].getAddress() ); // get the address
input
System.out.println(); // blank line for readability
System.out.print( ids[3].getCity() ); // get the city input
System.out.printf(", "); // Comma, Space on same line as
City, State and Zip for readability
System.out.print( ids[4].getState() ); // get the state input
System.out.printf(" "); // Same line for City, State and Zip
System.out.print( ids[5].getZip() ); // get the zip code input
System.out.println(); // blank line for readability
System.out.print( ids[0].getFname() ); // get the name again
for the statement of donations total
System.out.printf( "'s yearly donations is: $%,.2f\n", +
ids[8].getYearlyDonations() ); // display yearly donations total
//ids.; // Add another until 5 iterations
//} // end While and termination of loop @ 5 iterations only
System.out.println ( "Values: ");
for ( int i = 0; i < ids.length; i++ )
System.out.println ( ids[i] );
}
} // end
--
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