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 6850607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:08:43+00:00 2026-05-27T01:08:43+00:00

For my homework assignment, I’m supposed to create an ATM/Teller program which stores users

  • 0

For my homework assignment, I’m supposed to create an ATM/Teller program which stores users accounts in a text file. I require help reading the text file and storing certain parts of it in an array list.

import java.io.*;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.BufferedReader;

public class GetData
{
  public static void main(String[] args)
  {
    BufferedReader in = new BufferedReader(new FileReader("filefullofmoney.txt"));

    String strLine;
    int numberOfLines = 0;    
    while ((strLine = in.readLine()) != null)
    {
      numberOfLines++;
    }

    Database[] accounts = new Database[numberOfLines];
    String[] array1 = new String[3];

    int i;
    int j = 0;

    while (j < numberOfLines)
    {
      for (i=0; i < 2; i++)
      {
        array1[i] = in.readLine();     
      }
      accounts.add(new Database(array[0],array[1],array[2]));
    }
  }
}

class Database
{
  public String accountName;
  public int pin;
  public double balance;
}

The part I’m having trouble with is accounts.add(new Database(array[0],array[1],array[2]));

Basically my text file will be formatted in this way:

Account1 name
Account1 pin
Account1 balance
Account2 name
Account2 pin
Account2 balance
etc...

I want to be able to add the 3 lines of text for each account into one element on the arraylist.

I’m not sure how much of my could actually works because I can’t get it to compile.

Any help is greatly appreciated. Thanks

  • 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-27T01:08:44+00:00Added an answer on May 27, 2026 at 1:08 am

    A few problems with your code are:

    • You do not have a specified constructor for your Database class (which should be named Account).
    • You do not substring the lines, so you get along all the “Database#” prefixes.
      • And may I ask why you even have the prefixes there? They seem superfluous.
    • You do not cast the strings to the actual data types (int and double).
    • You loop over the content twice when you only need to do so once.
    • You do not have proper exception handling; you should never wrap everything in one catch(Exception).

    A possible solution to your code could be this (I have not tested if it actually works):

    private static String getLineContent(String value) {
        return value.substring(value.indexOf(' ') + 1);
    }
    
    public static void main(String[] args) {
        BufferedReader in;
        try {
            in = new BufferedReader(new FileReader("filefullofmoney.txt"));
        } catch (FileNotFoundException ex) {
            // TODO: Handle the error with a nice error message.
            return;
        }
    
        List<Account> accounts = new ArrayList<Account>();
    
        while (true) {
            try {
                String accountName = in.readLine();
    
                if (accountName == null) {
                    // We have no new accounts. So we exit.
                    break;
                }
    
                accountName = getLineContent(accountName);
                int pin = Integer.parseInt(getLineContent(in.readLine()));
                double balance = Double.parseDouble(getLineContent(in.readLine()));
    
                accounts.add(new Account(accountName, pin, balance));
            } catch (IOException ex) {
                // TODO: Handle the error with a nice message saying that the file is malformed.
            }
        }
    }
    
    class Account {
    
        public String accountName;
        public int pin;
        public double balance;
    
        public Account(String accountName, int pin, double balance) {
            this.accountName = accountName;
            this.pin = pin;
            this.balance = balance;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a homework assignment in which I'm supposed to create a vector of pointers
I finished my short file for a homework assignment which uses IO.popen(command).readlines to grab
I have a homework assignment where I need to take input from a file
I'm doing a basic homework assignment which looks like this: While input <> -1
As part of a homework assignment, I have to program a simple chess game
Wrote up a basic file handler for a Java Homework assignment, and when I
I'm doing a homework assignment in which I need to print out to the
I have done a homework assignment, here is the problem statement: Your program should
My homework assignment is to create a shell (done) with a history function (done)
A recent homework assignment I have received asks us to take expressions which could

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.