Hello, I've been trying to read txt from a file, and breaking each line into words and storing them in Array.
Example. txt file

C001 John Smith 999999
C002 Mary Agnes 888888

Here is my code.

public class Customer implements iCommand{

    static List<Customer> cust = new ArrayList<Customer>();

    static private String cID, String cName, cPhonenum;


    public Customer(String id, String name, String phone_num){

        this.cID = id;
        this.cName = name;
        this.cPhonenum = phone_num;

    }

    public static void main(String[] args){


        Scanner f = null;
        try {

            f = new Scanner(new File("C:/x.txt"));

        } catch (FileNotFoundException e) {

            System.out.println("unable to open file");
            e.printStackTrace();
        }

        while (f.hasNextLine()){

            String input = null;
            String[] words = input.split("");

            Customer client = new Server( cID, cName, cPhonenum);
            clients.add(client);

        }
    }
}

Can anyone help me because I haven't found what I'm looking for with my current code.

Thanks.

The most obvious problem is that you are breaking on the space between first and last name as well.
But when you create your client object you want to pass in the relevant strings from the array. In its most basic form it would look like this:

string input = f.ReadLine();
string[] words = input.split(" ");
Customer client = new Customer(words[0].toString(), words[1].ToString() + " " + words[2].toString(), words[3].toString());

But that assumes a first and last name and never less or more than that so it isn't very robust. You could look as a CSV (comma separated value) file and then split on the comma to avoid that.

Anyway, hope that helps.

There are couple of errors I found in your code...

  1. You have to import io and util packages in order to use I/O related classes and ArrayList class

    import java.io.*;
    import java.util.*;

  2. You don't have to put "String" in between variables when declaring them

    static private String cID, String cName, cPhonenum; // Incorrect
    static private String cID, cName, cPhonenum; // Correct

  3. You have not read the line from the Scanner and assigned it to "input"

    String input = null; // Incorrect
    String input = f.nextLine(); // Correct. Reads the next line from the file

  4. When splitting words from the input string, use <space> to split words

    String[] words = input.split(""); // Splits the characters in "input".
    String[] words = input.split(" ");// Splits the words in "input".

  5. Move the f.nextLine(), i.e. while loop inside the try-catch block, because nextLine() throws NoSuchElementException and IllegalStateException.
    (Refrence: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.