[英]Can't get Scanner to read from file
我正在命令行中制作大逃殺游戲,並且我在兩個文本文件中有機器人的名字和姓氏。 現在我試圖用 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”。 那只是意味着掃描儀由於某些原因無法讀取這兩個文件......
您可以修改 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()]);
姓氏數組類似。
此外,使用靜態的首要原因是線程安全第二個面向對象的概念是不明智的。
不要擔心有一天我們都在你的鞋子里,然后我們隨着時間的推移而學習。 沒什么大不了的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.