Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-读取文件并创建数组_Java_Arrays_String_Readfile - Fatal编程技术网

java-读取文件并创建数组

java-读取文件并创建数组,java,arrays,string,readfile,Java,Arrays,String,Readfile,我必须按照特定的格式使用扫描仪。我知道有更好的方法,但这是一个要求,我对java还不熟悉,并试图弄明白这一点。有一个包含客户名称、出生日期和其他信息的文件。我需要计算文件中的条目数,然后文件需要根据文件条目数创建一个数组,并将数组转换为字符串数组。有更多的代码我需要做,但我被困在这一部分。必须使用try/catch public class Customers { //////// MAIN //////// public static void main(String[] args)

我必须按照特定的格式使用扫描仪。我知道有更好的方法,但这是一个要求,我对java还不熟悉,并试图弄明白这一点。有一个包含客户名称、出生日期和其他信息的文件。我需要计算文件中的条目数,然后文件需要根据文件条目数创建一个数组,并将数组转换为字符串数组。有更多的代码我需要做,但我被困在这一部分。必须使用try/catch

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;
公共类客户{
////////主要////////
公共静态void main(字符串[]args){
File File=新文件(“customers.txt”);
int numEntries=countCustomers(文件);
Person[]customers=readIntoArray(文件,numCustomers);
int min=地点出生日期(客户);
System.out.println(“本月的生日:+customer[mon].getBirthdate());
}
//*统计档案中的客户数//
公共静态int countCustomers(文件f){
int i=0;
试一试{
扫描仪扫描=新扫描仪(f);
while(scan.hasNextLine()){
i++;
scan.nextLine();
}
}捕获(例外e){
System.out.println(“检查文件名”);
}
返回i;
}
//将数据读入数组并转换为字符串数组
公共静态客户[]readIntoArray(文件f,int num){
//num=countCustomers(f);
num=0;
试一试{
扫描仪输入=新扫描仪(f);
客户[]出生=新客户[num];
String[]strberth=新字符串[num];
while(num
好的。我有几点意见。 首先-您真的需要“strberth”数组吗?看起来您只写元素,而不从数组中读取。 第二个-'readIntoArray'方法始终返回null。 另外,你要数两次顾客,但只有一个就足够了。 那么,您真的需要一个包含客户的数组吗?因为您使用数组,所以您需要准确地知道客户的数量,因此您需要扫描文件两次。如果您使用ArrayList,则只需要扫描文件一次

我已经修复了下面代码中的问题

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);
  }
公共类客户{
////////主要////////
公共静态void main(字符串[]args){
File File=新文件(“customers.txt”);
Person[]customers=readIntoArray(文件,numCustomers);
int numEntries=customers.length;
int min=地点出生日期(客户);
System.out.println(“本月的生日:+customer[mon].getBirthdate());
}
public static Person[]readIntoArray(文件f,int num){
列出客户=新建ArraList();
试一试{
扫描仪输入=新扫描仪(f);
String[]strberth=新字符串[num];
while(scan.hasNextLine()){
添加(makeCustomer(scan.nextLine());
}
}捕获(例外e){
抛出新的运行时异常(e);
}
返回客户。toArray(人员[]::新建);
}

在帖子中嵌入代码snippit对不起,我对这一点还不熟悉,希望这篇帖子看起来更好