0

I have Googled this for a couple of days without much luck. I am trying to read a text file and use that information to populate the private fields of an array for a class object. I am new to Java and pretty new to programming in general.

What I've come up with for reading into the array seems really clunky and I feel there must be a better way, but I cannot find a good example for this particular kind of case case.

Creating a bunch of string variables was the only way I could get this to work. Perhaps main is a bad place to do this; perhaps Scanner is a poor choice here?

What better ways are there to implement this situation?

My text file that contains Strings and integers separated by whitespace on lines is similar to this:

Joe 2541 555-1212 345 1542 Type

Bob 8543 555-4488 554 1982 Type ... etc.

Here's my majority of my code thus far which is within main:

   Scanner in = new Scanner(new FileReader("accounts.txt")); //filename to import
   Accounts [] account = new Accounts [10];
   int i = 0; 
   while(in.hasNext())
   {          
    account[i] = new Accounts();
    String name = in.next();
    String acct_num = in.next();
    String ph_num = in.next();
    String ss_num = in.next();
    int open_bal = in.nextInt();
    String type = in.next();

    account[i].setName(name);
    account[i].setAcctNum(acct_num);
    account[i].setPhoneNum(ph_num);
    account[i].setSSNum(ss_num);
    account[i].setOpenBal(open_bal);
    account[i].setType(type);
    i++;
   }

class Accounts
{
  public Accounts()
  { 
  }

  public Accounts(String n, String a_num, String ph_num, 
  String s_num, int open_bal, String a_type, double close_bal)
  {
  name = n;
  account_number = a_num;
  phone_number = ph_num;
  ssn = s_num;
  open_balance = open_bal;
  type = a_type;
  close_balance = close_bal;
  }
  public String getName()
  {
    return name;
  } 
  public void setName(String field)
  {
    name = field;
  }
  public String getAcctNum()
  {
    return account_number;
  }
  public void setAcctNum(String field)
  {
    account_number = field;
  }
  //And so forth for the rest of the mutators and accessors

  //Private fields             
  private String name;
  private String account_number;
  private String phone_number;
  private String ssn;
  private int open_balance;
  private String type;
  private double close_balance;
} 
jhovanec
  • 544
  • 5
  • 6

2 Answers2

0

I believe you need to split each line in order to get the data contained in each line. You can use the split() of the string class which will return a string[]. Then you can go through each index of the string array and pass them to the mutator methods of the account class.

Something like this maybe.

while(in.hasNext())
{
    // will take each line in the file and split at the spaces.
    String line = in.next();
    String[] temp = line.split(" ");

    account[i].setName(temp[0]);
    account[i].setAcctNum(temp[1]);
    account[i].setPhoneNum(temp[2] + "-" + temp[3]);
    account[i].setSSNum(temp[4]);
    account[i].setOpenBal((int)temp[5]);
    account[i].setType(temp[6]);

    // will account for blank line between accounts.
    in.next();
    i++;
 }

The phone number gets split into two separate indices so you have to rejoin the phone number by accounting for the first 3 digits being in one index and the last 4 being in the next.

Jason White
  • 3,894
  • 1
  • 15
  • 24
0
Community
  • 1
  • 1
wypieprz
  • 7,048
  • 3
  • 34
  • 40