Search code examples
javaarraysstringreadfile

java - read file and create array


I have to follow a specific format and use scanner. I know there are better methods, but this is a requirement and I am new to java and trying to figure this out. There is a file with customer name, birth date, and other information. I need to count the number of entries in the file, then the file needs to create an array based on the number of file entries and convert the array to a string array. There is more code I need to do, but I am stuck on this part. The try/catch has to be used.

public class Customers {
  //////// MAIN ////////
  public static void main(String[] args) {
    File file = new File("customers.txt");
    int numEntries = countCustomers(file);
    Person[] customers = readIntoArray(file, numCustomers);
    int min = locateBirthdate(customers);
    System.out.println("Birthdays this month: " + customer[mon].getBirthdate());

  }
  //* Counts customers in the file.//
  public static int countCustomers(File f) {
    int i = 0;
    try {
      Scanner scan = new Scanner(f);
      while (scan.hasNextLine()) {
        i++;
        scan.nextLine();
      }
    } catch (Exception e) {
      System.out.println("Check filename.");
    }

    return i;
  }
  //read data into array and convert into string array

  public static Customer[] readIntoArray(File f, int num) {
      //num = countCustomers(f);
      num = 0;
      try {
        Scanner input = new Scanner(f);
        Customer[] birth = new Customer[num];
        String[] strBirth = new String[num];
        while (num < countCustomers(f)) {
          strBirth[num] = input.nextLine();
          birth[num] = makeCustomer(strBirth[num]);
          num++;
          System.out.println(num);
        }

      } catch (Exception e) {
        System.out.println("Error");
      }
      return null;

Solution

  • Ok. I have several comments. First - do you really need 'strBirth' array? Looks like you only write elements but not read from the array. Second - 'readIntoArray' method always returns null. Also, you count customers twice, but only one is enough. Then, do you really need an array with customers? Since you use an array, you need to know exactly the count of customers and therefore you need to scan the file twice. If you use ArrayList, you need to scan file only one time.

    I have fixed the issues in the code below.

    public class Customers {
      //////// MAIN ////////
      public static void main(String[] args) {
        File file = new File("customers.txt");
        Person[] customers = readIntoArray(file, numCustomers);
        int numEntries = customers.length;
        int min = locateBirthdate(customers);
        System.out.println("Birthdays this month: " + customer[mon].getBirthdate());
    
      }
    
      public static Person[] readIntoArray(File f, int num) {
          List<Customer> customers = new ArraList<>();
          try {
            Scanner input = new Scanner(f);
            String[] strBirth = new String[num];
            while (scan.hasNextLine()) {
              customers.add(makeCustomer(scan.nextLine()));
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
          return customers.toArray(Person[]::new);
      }