有人知道这个方法中的字符串解析失败了,我不知道。 我真的想把这个方法付诸行动,在我正在研究的项目中。 谢谢你,亲切的问候。
public static void loadActiveGameStateSprites(){
try {
// Clears and sets resources
activeGameStateSprites.clear();
try {
fileScanner = new Scanner(new File("res/allGameStatesSpriteStats.txt"));
}
catch (FileNotFoundException e) {
System.out.println("An error has occured during the file Read operation of method loadActiveGameStateManager");
}
// creates all sprites, sort and reads x, y, image file path,
while (fileScanner.hasNextLine()){
String readLine = fileScanner.nextLine();
System.out.println("test readLine: " + readLine);
String[] lineValues = readLine.split(",");
for (String lineValue : lineValues) {
lineValue.trim();
System.out.println("test lineValue: " + lineValue);
}
int x = Integer.parseInt(lineValues[0]); // <--------- does not work
// int y = Integer.parseInt(lineValues[1]); <--------- does not work
// String spritePath = lineValues[2]; <--------- does not work
// Platformable p = new Platformable(x, y, 80, 80, spritePath); <--------- therefor cannot not work
//activeGameStateSprites.add(p);
if (activeLastScannedValue <= activeNextScanValue){ // if (!(activeLastScannedValue > activeNextScanValue)){
activeLastScannedValue++;
} else {
numScanValuesToBeReadActiveIndex++;
activeNextScanValue = numScanValuesToBeReadPerGameStates[numScanValuesToBeReadActiveIndex];
}
}
} catch (NumberFormatException e) {
System.out.println("An error has occured in method loadGameStateSprites()");
}
}
txt文件如下所示(没有空行):
100,50,images / testImage1.png
20,30,images / testImage2.png
40,50,images / testImage3.png
60,70,images / testImage4.png
80,10,images / testImage5.png
40,40,images / testImage6.png
50,90,images / testImage7.png
300,500,images / testImage8.png
读取的第一行的所有值都正确存储 我把它们打印出来并进行了测试。
例如: liveValues [0] = 100;
liveValues [1] = 50;
liveValues [2] = images / testImage1.png;
&lt; ----------------------------此时捕捉到异常e
catch (NumberFormatException e) {
System.out.println("An error has occured during the file Read operation of method loadActiveGameStateManager");
}
打印stacktrace uotputs:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at GameStateManager.loadActiveGameStateSprites(GameStateManager.java:89)
at GameStateManager.loadActiveGameState(GameStateManager.java:59)
at GameStateManager.<init>(GameStateManager.java:34)
at GamePanel.<clinit>(GamePanel.java:23)
at GameFrame.<clinit>(GameFrame.java:9)
建立成功(总时间:19秒)
答案 0 :(得分:1)
我制作了一个简化版的代码,它还检查一行是否为空(即使你提到它不是空白)。我的代码工作正常。你可以在这里插入你的代码,使用所有那些拆分代币来制作精灵等。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Games {
public static void main(String[] args) {
Scanner fileScanner;
String readLine;
String[] lineValues;
int x = 0;
int y = 0;
String spritePath = null;
try {
fileScanner = new Scanner(new File("C:/temp/a.txt"));
while (fileScanner.hasNextLine()) {
readLine = fileScanner.nextLine();
if (readLine.length() != 0) {
lineValues = readLine.split(",");
x = Integer.parseInt(lineValues[0].trim());
y = Integer.parseInt(lineValues[1].trim());
spritePath = lineValues[2].trim();
System.out.println(x + "," + y + "," + spritePath);
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}