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