user3659502
user3659502

Reputation: 1

How to tokenize a file and input the data into an array?

I have a file of information separated by commas of which I need to tokenize and place into arrays.

The file has info such as

14299,Lott,Lida,22 Dishonest Dr,Lowtown,2605
14300,Ryder,Joy,19 Happy Pl,Happyville,2701

and so forth. I need to tekonize those pieces of information of which is separated by a comma. I'm not sure how to write out the tokenizer code to make it separate. I've managed to count the amount of lines in the document with;

File customerFile = new File("Customers.txt");
    Scanner customerInput = new Scanner(customerFile);
    //Checks if the file exists, if not, the program is closed
    if(!customerFile.exists()) {
      System.out.println("The Customers file doesn't exist.");
      System.exit(0);
    }
    //Counts the number of lines in the Customers.txt file
    while (customerInput.hasNextLine()) {
      count++;
      customerInput.nextLine();
    }

And I also have the class of which I'll be placing the tokenized info into;

public class Customer {
  private int customerID;
  private String surname;
  private String firstname;
  private String address;
  private String suburb;
  private int postcode;
public void CustomerInfo(int cID, String lname, String fname, String add, String sub, int PC) {
  customerID = cID;
  surname = lname;
  firstname = fname;
  address = add;
  suburb = sub;
  postcode = PC;
}

But after this point I'm not sure how to place the info into the arrays of the customer. I've tried this but it's not right;

for(i = 0; i < count; i++) {
      Customer cus[i] = new Customer;
    }

It's telling me the 'i' and the new Customer are errors as it 'can't convert Customer to Customer[]' and 'i' has an error in the token.

Upvotes: 0

Views: 704

Answers (2)

raytong
raytong

Reputation: 31

If it is a CSV file instead of a simple comma separated flie, maybe consider some library like:

Upvotes: 0

huidube
huidube

Reputation: 410

first, you need to declare the Customer Array:

Customer[] cus = new Customer[count];

Now, the programm knows, how much space it has to allocate on the memory. Then, you can use your loop, but you have to call the constructor of the class Customer and give him all the information it needs to create a new one :

for(i = 0; i < count; i++) {
  Customer cus[i] = new Customer(cID, lname, fname, add, sub, PC);
}

Another thing you would be asking yourself about is, how do i get the data from the Strings/lines into the array.

for that, you should write all lines in an ArrayList. Like this.

ArrayList<String> strList = new ArrayList<String>();
while (customerInput.hasNextLine()) {
    count++;
    strList.add(customerInput.nextLine());
}

Now you got all lines as Strings in an ArrayList. But you want to give the single values of each String to your constructor.

take a look at the split method from Strings. (How to split a string in Java).

With split() you can split one line like this:

String[] strArray = "word1,word2,word3".split(",");

then in the strArray you can find your data:

strArray[0] would have the value "word1";
strArray[1] = "word2"; 

and so on

Upvotes: 1

Related Questions