1

I would like to read the text file (Customer.txt) just like below. Note that <TAB> indicates the TAB character, i.e. U+0009

yx<TAB>Yong<TAB>123<TAB>Male<TAB>2002<TAB>999<TAB>jay@<TAB>NO234
paul<TAB>Paul Tan<TAB>123<TAB>Male<TAB>2002<TAB>999<TAB>kkk<TAB>nnn

And below is the code that I wrote to store in the ArrayList. Is that correct?

public static void readCustomer() throws Exception {
    try {
        Scanner s = new Scanner(new File("Customer.txt")).useDelimiter("\t");
        while (s.hasNext()) {
            String userName = s.nextLine();
            String fullName = s.nextLine();
            String password = s.nextLine();
            String gender = s.nextLine();
            String birthyear = s.nextLine();
            String phone = s.nextLine();
            String email = s.nextLine();
            String address = s.nextLine();
            s.nextLine();
            allCustomer.add(new Customer(userName, fullName, password, gender, birthyear, phone, email, address));
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error in read");
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
JYX
  • 25
  • 3
  • Based on your question suggestion, I changed `s.nextLine()` to `s.next()`, but it still show `Error in read` – JYX Aug 29 '22 at 04:17

2 Answers2

0

I suggest that you read the file line by line and call method split to separate it into fields.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Customer {
    private String userName;
    private String fullName;
    private String password;
    private String gender;
    private String birthyear;
    private String phone;
    private String email;
    private String address;

    public Customer(String userName,
                    String fullName,
                    String password,
                    String gender,
                    String birthyear,
                    String phone,
                    String email,
                    String address) {
        this.userName = userName;
        this.fullName = fullName;
        this.password = password;
        this.gender = gender;
        this.birthyear = birthyear;
        this.phone = phone;
        this.email = email;
        this.address = address;
    }

    public static void main(String[] args) {
        List<Customer> allCustomer = new ArrayList<>();
        try (Scanner s = new Scanner(new File("Customer.txt"))) {
            while (s.hasNextLine()) {
                String line = s.nextLine();
                String[] fields = line.split("\t");
                String userName = fields[0];
                String fullName = fields[1];
                String password = fields[2];
                String gender = fields[3];
                String birthyear = fields[4];
                String phone = fields[5];
                String email = fields[6];
                String address = fields[7];
                allCustomer.add(new Customer(userName,
                                             fullName,
                                             password,
                                             gender,
                                             birthyear,
                                             phone,
                                             email,
                                             address));
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

Note that the above code uses try-with-resources to ensure that the file Customer.txt is closed.

Edit

Here is an alternative solution that uses records, streams, method references and NIO.2

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public record Customer(String userName,
                       String fullName,
                       String password,
                       String gender,
                       String birthyear,
                       String phone,
                       String email,
                       String address) {
    private static final String DELIMITER = "\t";

    private static Customer parseLine(String line) {
        String[] fields = line.split(DELIMITER);
        return new Customer(fields[0],
                            fields[1],
                            fields[2],
                            fields[3],
                            fields[4],
                            fields[5],
                            fields[6],
                            fields[7]);
    }

    public static void main(String[] args) {
        try (Stream<String> lines = Files.lines(Paths.get("Customer.txt"))) {
            List<Customer> allCustomer = lines.map(Customer::parseLine)
                                              .collect(Collectors.toList());
            allCustomer.forEach(System.out::println);
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

Running the above code produces the following output:

Customer[userName=yx, fullName=Yong, password=123, gender=Male, birthyear=2002, phone=999, email=jay@, address=NO234]
Customer[userName=paul, fullName=Paul Tan, password=123, gender=Male, birthyear=2002, phone=999, email=kkk, address=nnn]
Abra
  • 19,142
  • 7
  • 29
  • 41
  • Hi, may I know how to print out the `ArrayList` content? – JYX Aug 29 '22 at 05:17
  • @Jaydeb you asked: _may I know how to print out the ArrayList content?_ `System.out.println(allCustomer);` But before that, I suggest that you override method `toString` in class `Customer`. – Abra Aug 29 '22 at 09:33
0

for some reason \t isn't working as a delimiter with scanner , but you know that <TAB> is actually is 4 white spaces . so you can use that information to make a new regex with this information . so here is the text file containing data:

yx    Yong    123    Male    2002    999    jay@    NO234
paul    Paul Tan    123    Male    2002    999    kkk    nnn

where every data is separated by 4 white spaces which is equivalent to TAB ,


also this is the new regex I used : "[\\s]{4}|\r\n" where

  • | means or
  • [\\s]{4} means 4 white spaces
  • \r\n means a newline with Carriage return

which means delimit if you find 4 white spaces or a new line with Carriage return.

and here is the new code edited :

    import javax.swing.*;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    public class Main {

        public static void main(String[] args) {
            readCustomer();
        }

        static List<Customer> allCustomer = new ArrayList<>();
        public static void readCustomer() {
            try {
                Scanner s = new Scanner(new File("Customer.txt"));
                s.useDelimiter("[\\s]{4}|\r\n");
                while (s.hasNextLine()) {
                    String userName = s.next();
                    String fullName = s.next();
                    String password = s.next();
                    String gender = s.next();
                    String birthyear = s.next();
                    String phone = s.next();
                    String email = s.next();
                    String address = s.next();
                    allCustomer.add(new Customer(userName, fullName, password, gender, birthyear, phone, email, address));
                }
                for (Customer c : allCustomer)
                    System.out.println(c.toString());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error in read");
            }
        }

    }

    class Customer
    {
        private String userName;
        private String fullName;
        private String password;
        private String gender;
        private String birthyear;
        private String phone;
        private String email;
        private String address;

        public Customer(String userName, String fullName, String password, String gender, String birthyear, String phone, String email, String address) {
            this.userName = userName;
            this.fullName = fullName;
            this.password = password;
            this.gender = gender;
            this.birthyear = birthyear;
            this.phone = phone;
            this.email = email;
            this.address = address;
        }

        @Override
        public String toString() {
            return "Customer{" +
                    "userName='" + userName + '\'' +
                    ", fullName='" + fullName + '\'' +
                    ", password='" + password + '\'' +
                    ", gender='" + gender + '\'' +
                    ", birthyear='" + birthyear + '\'' +
                    ", phone='" + phone + '\'' +
                    ", email='" + email + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

and here is a picture of the result:

enter image description here

abdo Salm
  • 1,678
  • 4
  • 12
  • 22
  • _but you know that is actually is 4 white spaces_ Not according to [Wikipedia](https://en.wikipedia.org/wiki/Tab_key) – Abra Aug 29 '22 at 09:55