Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:22:50+00:00 2026-05-31T22:22:50+00:00

Part4: Read a String aName, an int aPin, a double aWithdraw, and a double

  • 0

Part4: Read a String aName, an int
aPin, a double aWithdraw, and a double aDeposit. Then search in anotherArray to find a customer record with name equal to aName and pin equal to aPin. If the record is found, do the withdrawal and
deposit transactions using aWithdraw and aDeposit amounts and update the record’s balance. Otherwise
print an error message. This process can be repeated for other transactions on other records.
I’ve done a little bit, I’m getting a error on the ‘if statement’.

Part5: Write the customer records from anotherArray in the binary file customer_records.dat after updating the customer records.

Please Help! I would really appreciate it!

  import java.io.Serializable;
  import java.util.Scanner;

/**
 Serialized class for data on endangered species.
 Includes a main method.
*/
public class CustomerRecord implements Serializable
{
private String name;
private int pin;
private int account;
private static double balance;

public CustomerRecord( )
{
    name = null;
    pin = 0;
    account = 0;
    balance = 0.00;
}

public CustomerRecord(String initialName, int initialPin, int initialAccount, double initialBalance)
{
    name = initialName;

    if (initialPin >= 1111 && initialPin <= 9999)
            pin = initialPin;
    else
    {
            System.out.println("ERROR: Pin is not acceptable.");
            System.exit(0);
    }

if (initialAccount >= 400111 && initialAccount <= 500111)
        account = initialAccount;
else
{
    System.out.println("ERROR: Account number is not acceptable.");
            System.exit(0);
}

if (initialBalance >= 0)
        balance = initialBalance;
else
{
    System.out.println("ERROR: Initial Balance is not acceptable.");
            System.exit(0);
}
}

public String toString()
{
    return ("Name = " + name
            + "  Account = " + account
    + "  Balance = " + "$" + balance + "\n");
}

public void setCustomerRecord( )
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("\nEnter new customer's name: ");
    name = keyboard.nextLine( );

    System.out.println("Enter new customer's pin: ");
    pin = keyboard.nextInt( );
    while (pin < 1111 || pin > 9999)
    {
        System.out.println("Pin should be in the range of 1111-9999.");
        System.out.println("Reenter pin:");
        pin = keyboard.nextInt( );
    }

    System.out.println("Enter new customer's account no: ");
account = keyboard.nextInt( );
    while (account < 400111 || pin > 500111)
    {
        System.out.println("Account should be in the range of 400111-500111.");
        System.out.println("Reenter account number:");
        account = keyboard.nextInt( );
    }

System.out.println("Enter new customer's initial balance: ");
    balance = keyboard.nextDouble( );
while (balance < 0)
    {
        System.out.println("Initial balance should be positive or zero.");
        System.out.println("Reenter initial balance:");
        balance = keyboard.nextDouble( );
    }
}

public void writeOutput( )
{
     System.out.print("Name = " + name + "\t");
     System.out.print("Account = " + account + "\t");
     System.out.print("Balance = " + "$" + balance + "\n");
}

public String getName( )
{
    return name;
}

public int getPin( )
{
    return pin;
}

public int getAccount( )
{
    return account;
}



public void setBalance(double amount)
{
    balance = amount;
}

public static void deposit(double aDeposit)
{
balance = balance + aDeposit;
}
public static void withdraw(double aWithdraw)
{
    if 
    ( balance >= aWithdraw)
        balance = balance - aWithdraw;
    else if
    ( balance < aWithdraw)
        System.out.println("Cannot withdarw amount.");
    return;
}
     public double getBalance( )
   {
    return balance;
}
public boolean equal(CustomerRecord otherObject)
{
    return (name.equalsIgnoreCase(otherObject.name) &&
           (pin == otherObject.pin) &&
           (account == otherObject.account) &&
           (balance == otherObject.balance));
}
 }

import java.io.FileInputStream;
    import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
   import java.util.Scanner;

 public class BankCustomers
   {
public static void main(String[] args)
{   
    //----------------------------------------------------------------

    //part1: Create array of customer records and write them 
    //in the file customer_record.dat


    CustomerRecord[] oneArray = new CustomerRecord[100];
    oneArray[0] = new CustomerRecord("John Gray", 2222, 400222, 10000.00);
    oneArray[1] = new CustomerRecord("Ann Black", 3333, 400333, 1500.00);
    oneArray[2] = new CustomerRecord("Mary White", 4444, 400555, 16500.00);
    oneArray[3] = new CustomerRecord("Jack Green", 7777, 400888, 100.00);

    int index = 4;

    String fileName = "customer_records.dat";
    System.out.println("---Open file: " + fileName);
    System.out.println("---Customer records written to file: " + fileName);
    System.out.println("---Close file: " + fileName);
    try
    {
        ObjectOutputStream outputStream =
              new ObjectOutputStream(
                  new FileOutputStream(fileName));
        outputStream.writeObject(oneArray);
        outputStream.close( );
    }
    catch(IOException e)
    {
        System.out.println("Error writing to file " +
                            fileName + ".");
        System.exit(0);
    }
    System.out.println("\n");


    //-------------------------------------------------------------------
    //Part2: Read from the file customer_record.dat and save 
    //the customer records in anotherArray


    System.out.println("---Open file: " + fileName);
    System.out.println("---Customer records read from file: " + fileName);
    System.out.println("---Close file: " + fileName);
    CustomerRecord[] anotherArray = null;
    try
    {
        ObjectInputStream inputStream = 
                   new ObjectInputStream(
                           new FileInputStream(fileName));
        anotherArray = (CustomerRecord[])inputStream.readObject( );
        inputStream.close( );
    }
    catch(Exception e)
    {
        System.out.println("Error reading file " + 
                            fileName + ".");
        System.exit(0);
    }
            //Print the records on screen
    for (int i = 0; i < index; i++)
        System.out.print(anotherArray[i]);


    //------------------------------------------------
    //Part3: Add a new customer record to anotherArray

    anotherArray[index] = new CustomerRecord();
    anotherArray[index].setCustomerRecord();
    index++;
            //Print the records on screen
    for (int i = 0; i < index; i++)
        System.out.print(anotherArray[i]);


    //------------------------------------------------
    //Part4: Find a customer record from anotherArray 
    //to do transaction(s) and update the record's balance

    char repeat; // User control to repeat or quit


    do{   


        System.out.println("Enter the name");
        String aName;
        Scanner keyboard = new Scanner(System.in);
        aName = keyboard.nextLine();

        System.out.println("Enter the pin");
        int aPin;
        aPin = keyboard.nextInt();


        System.out.println("Enter the amount you wish to withdraw");
        double aWithdraw;
        aWithdraw = keyboard.nextDouble();
        CustomerRecord.withdraw(aWithdraw);

        System.out.println("Enter the amount you wish to deposit");
        double aDeposit;
        aDeposit = keyboard.nextDouble();
        CustomerRecord.deposit(aDeposit);

             for
             (int i = 0; i < anotherArray.length; i++) {
              CustomerRecord record = anotherArray[i];
              if
              ((record.getName().equalsIgnoreCase(aName)) && (record.getPin() == (aPin)))
    {


                System.out.println(record);
                record.getBalance();

              }

              }


        System.out.println("\nAnother Transaction? (y for yes)");
        repeat = keyboard.next().charAt(0);

    }
    while
        (

         repeat == 'y' || repeat == 'Y') ;

        //Print the records on screen

    { for (int i = 0; i < index; i++)
        System.out.print(anotherArray[i]);
    }


    //------------------------------------------------
    //Part5: Write the customer records from anotherArray 
    //in the file customer_record.dat 

    String fileName1 = "customer_records.dat";
    System.out.println("---Open file: " + fileName1);
    System.out.println("---Customer records written to file: " + fileName1);
    System.out.println("---Close file: " + fileName1);
    try
    {
        ObjectOutputStream outputStream =
              new ObjectOutputStream(
                  new FileOutputStream(fileName1));
        outputStream.writeObject(anotherArray);
        outputStream.close( );
    }
    catch(IOException e)
    {
        System.out.println("Error writing to file " +
                            fileName1 + ".");
        System.exit(0);
    }
    System.out.println("\n");

    //End of program
    System.out.println("\n---End of program.");
}
}       

Edit
Error message:
Exception in thread “main” java.lang.NullPointerException at BankCustomers.main(BankCustomers.java:115)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:22:52+00:00Added an answer on May 31, 2026 at 10:22 pm

    This is really no surprise here – you are first doing this:

    CustomerRecord[] oneArray = new CustomerRecord[100];
    oneArray[0] = new CustomerRecord("John Gray", 2222, 400222, 10000.00);
    oneArray[1] = new CustomerRecord("Ann Black", 3333, 400333, 1500.00);
    oneArray[2] = new CustomerRecord("Mary White", 4444, 400555, 16500.00);
    oneArray[3] = new CustomerRecord("Jack Green", 7777, 400888, 100.00);
    

    Then you create one by hand (all well and good), but then you try to iterate through the ENTIRE set of customers!

    oneArray[0] = 'John Gray';
    oneArray[1] = 'Ann Black';
    oneArray[2] = 'Mary White';
    oneArray[3] = 'Jack Green';
    oneArray[4] = 'WHowever you made up';
    oneArray[5] to oneArray[99] = NULL;
    

    BAM, you get a NullPointerException when trying to get the pin and name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with reading empty string in C. I want to read
I am trying to read unicode string from a console in C#, for the
I have read RSS and get html string like at http://pastebin.com/eWQ6gUe2 which i display
I need to somehow read an integer within a string. The string will be
I would like to read an file into a string. I am looking for
I am trying to read a string that includes a backslash in it from
I have a String Array which is contain dates that read from a CSV
I have the following 'td'element, and want to read out the string part (cell
i've got a long string which is space delimited (read in from a txt
I have a string I read from a configuration file. Structure of the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.