无法让扫描仪读取文件

时间:2019-02-28 19:27:51

标签: java arrays file oop

我正在命令行中制作“皇家大逃杀”游戏,并且在两个文本文件中包含了机器人的名字和姓氏。现在,我尝试使用2个扫描仪读取这两个文件,然后将“名字”放在一个数组中,然后将“姓氏”放在另一个数组中。

游戏是面向对象的,因此有3个文件:

文件#1(应用程序)

package tp2;

public class App {

    public static void main(String[] args) throws InterruptedException {
        Game game = new Game();
        game.start();
    }    
}

文件#2(游戏)

package tp2;

public class Game {

    private Level currentLevel;
    private Player player;

    public void start() throws InterruptedException {
        World.createNameArrays();
        World.printNamesArrays();              
    }
}

文件#3(世界)

package tp2;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class World {
    private static final int MAX_PLAYERS = 42;
    private static String firstNamesArray[] = new String[MAX_PLAYERS];
    private static String lastNamesArray[] = new String[MAX_PLAYERS];
    private static Player players[] = new Player[MAX_PLAYERS];

    private static Scanner scannerFirstNames;
    private static Scanner scannerLastNames;

    public static void printNamesArrays() {
        System.out.println("");
        System.out.println("First names available");
        System.out.println("---------------------");
        for (int i = 0; i < MAX_PLAYERS; ++i) {
            System.out.println(firstNamesArray[i]);
        }
        System.out.println("");
        System.out.println("Lats names available");
        System.out.println("---------------------");
        for (int i = 0; i < MAX_PLAYERS; ++i) {
            System.out.println(lastNamesArray[i]);
        }
    } 

    public static void createNameArrays() {        
        openNameFiles();
        int i = 0;
        while (scannerFirstNames.hasNext() && scannerLastNames.hasNext()) {
            System.out.println("**********Test*************");
            firstNamesArray[i] = scannerFirstNames.nextLine();
            lastNamesArray[i] = scannerLastNames.nextLine();
            ++i;            
        }
        closeNameFiles();
    }

    public static void closeNameFiles() {
        scannerFirstNames.close();        
        scannerLastNames.close();
    }

    public static void openNameFiles() {
        try {
            scannerFirstNames = new Scanner(new File("firstnames.txt"));
        } catch(FileNotFoundException e) {
            System.out.println("Error! File not found.");
        }
        try {
            scannerLastNames = new Scanner(new File("lastnames.txt"));
        } catch(FileNotFoundException e) {
            System.out.println("Error! File not found.");
        }        
    }
}

为澄清起见,我制作了一种方法来打印两个数组中的名字和姓氏,但是当我执行该方法时,它只是打印“ NULL”。那只是意味着扫描器由于某些原因无法读取两个文件...

1 个答案:

答案 0 :(得分:1)

您可以修改createNamesArray()函数并摆脱closeNameFiles()和openNameFile()

我已经更新了World.java(以防您使用java:8)

public class World {

  private static final int MAX_PLAYERS = 42;
  private static String firstNamesArray[] = new String[MAX_PLAYERS];
  private static String lastNamesArray[] = new String[MAX_PLAYERS];
  private static int firstNameEntriesInFile;
  private static int lastNameEntriesInFile;

  public static void printNamesArrays() {
    System.out.println("");
    System.out.println("First names available");
    System.out.println("---------------------");
    for (int i = 0; i < MAX_PLAYERS && i < firstNameEntriesInFile; ++i) {
      System.out.println(firstNamesArray[i]);
    }
    System.out.println("");
    System.out.println("Lats names available");
    System.out.println("---------------------");
    for (int i = 0; i < MAX_PLAYERS && i < lastNameEntriesInFile; ++i) {
      System.out.println(lastNamesArray[i]);
    }
  }

  public static void createNameArrays() {
    try {
      String[] fNames = Files.lines(new File("fullpath/firstname.txt").toPath()).toArray(String[]::new);
      if (fNames.length == 0) {
        throw new RuntimeErrorException(null, "no first name entry found in the given file");
      } else {
       firstNameEntriesInFile=fNames.length;
       firstNamesArray=fNames;
      }
      String[] lNames = Files.lines(new File("fullpath/lastname.txt").toPath()).toArray(String[]::new);
      if (lastNamesArray.length == 0) {
        throw new RuntimeErrorException(null, "no last name entry found in the given file");
      }else {
        lastNameEntriesInFile=lNames.length;
        lastNamesArray=lNames;
       }
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println("file can't be accessed: cause " + e.getMessage());
    }
  }
}

还是,如果您愿意,我的建议是使用FileReader

我建议使用ArrayList来处理动态大小调整,而数组将需要预先定义的大小,您可能不知道。查看您的代码,我发现您已经定义了大小。

BufferedReader in = new BufferedReader(new FileReader("fullpath/firstnames.txt"));
String str;

List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
    list.add(str);
}

firstNamesArray = list.toArray(new String[list.size()]);

与姓氏数组类似。

此外,首先使用 static 也是最不明智的选择,其次是面向对象的面向对象的线程安全。

别担心,有一天我们都会穿上鞋子,然后随着时间的推移我们就会学到东西。没什么大不了!