为什么我的程序在读取这个文件输入时抛出空异常?

huangapple go评论90阅读模式
英文:

Why is my program throwing by a null exception when reading this file input?

问题

以下是翻译好的代码部分:

public static void main(String[] args) throws IOException {
    int[] boardGame = null;
    Scanner scnr = new Scanner(System.in);
    String fileName;
    String LogFile;
    
    try {
        System.out.println("输入游戏板数据输入文件名:");
        fileName = scnr.next(); 
        
        boardGame = createGameboard(fileName); // 这里出现问题,抛出异常并输出单词 "null"
        System.out.println();
        LogFile = writeLogFile(fileName, boardGame);
        if(LogFile.equals("null")) {
            throw new Exception("程序将继续没有日志文件。");
        }
        else {
            System.out.println("日志文件 " + LogFile + " 创建成功");
        }
        System.out.println();
    } catch (IOException excpt) {
        System.out.println(excpt.getMessage());
        System.out.println("没有游戏板无法进行游戏,因此程序退出");
    } catch (Exception excpt) { // 在这里捕获并显示单词 "null"
        System.out.println(excpt.getMessage()); 
    }
}

public static int[] createGameboard(String nameFile) throws IOException {
    int[] gameBoard = null;
    File gameFile = new File(nameFile);
    Scanner inFS = new Scanner(gameFile);
    int boardSize;
    int numLadders = 0;
    int numChutes = 0;
    int index;
    int value;
    
    boardSize = inFS.nextInt();
    boardSize = boardSize;
    gameBoard = new int[boardSize];
    while(inFS.hasNextInt()) {
        index = inFS.nextInt();
        value = inFS.nextInt();
        gameBoard[index] = value;
        if (value > 0) {
            numLadders++;
        }
        else if(value < 0) {
            numChutes++;
        }
        else {
        }        
    }
    
    System.out.println("游戏板设置为 " + (boardSize - 1) + " 个方格");
    System.out.println("  " + numChutes + " 个方格有滑道");
    System.out.println("  " + numLadders + " 个方格有梯子");
           
    return gameBoard;
}

希望这可以帮助你解决问题。如果你需要更多的帮助,请随时提问。

英文:

Writing a program to play the game Chutes and Ladders by inputting different ".txt" files and reading their data and outputting it to virtual game board. There is a lot more to the program, however, this part is hanging me up. Whenever the program tried to read the .txt file it throws an Exception and outputs the word "null".

   public static void main(String[] args) throws IOException {
int[] boardGame = null;
Scanner scnr = new Scanner(System.in);
String fileName;
String LogFile;
try {
System.out.println(&quot;Enter gameboard data input filename:&quot;);
fileName = scnr.next(); 
boardGame = createGameboard(fileName); //hangup appears to be here and throws Exception
System.out.println();
LogFile = writeLogFile(fileName, boardGame);
if(LogFile == &quot;null&quot;) {
throw new Exception(&quot;Program will continue without a log file.&quot;);
}
else {
System.out.println(&quot;Log file &quot; + LogFile + &quot; successfully created&quot;);
}
System.out.println();
} catch (IOException excpt) {
System.out.println(excpt.getMessage());
System.out.println(&quot;Cannot play without gameboard, so program exiting&quot;);
} catch (Exception excpt) {                 //which is caught here and displays the word null
System.out.println(excpt.getMessage()); 
}
}
public static int[] createGameboard(String nameFile) throws IOException {
int[] gameBoard = null;
File gameFile = new File(nameFile);
Scanner inFS = new Scanner(gameFile);
int boardSize;
int numLadders = 0;
int numChutes = 0;
int index;
int value;
boardSize = inFS.nextInt();
boardSize = boardSize;
gameBoard = new int[boardSize];
while(inFS.hasNextInt()) {
index = inFS.nextInt();
value = inFS.nextInt();
gameBoard[index] = value;
if (value &gt; 0) {
numLadders++;
}
else if(value &lt; 0) {
numChutes++;
}
else {
}        
}
System.out.println(&quot;Gameboard setup with &quot; + (boardSize - 1) + &quot; squares&quot;);
System.out.println(&quot;  &quot; + numChutes + &quot; squares have a chute&quot;);
System.out.println(&quot;  &quot; + numLadders + &quot; squares have a ladder&quot;);
return gameBoard;
}
}

Sample ".txt" file being used:
31 (boardSize)
3 19 (square number : move up or down 'x' spaces)
5 3
11 15
20 9
17 -13
19 -12
21 -12
27 -26

答案1

得分: 0

我认为问题出在这里

boardSize = inFS.nextInt();

检查 inFS(文件)是否有下一个输入作为整数。

用以下方式更正

if(inFS.hasNextInt())
boardSize = inFS.nextInt();
英文:

I think the problem lies here

boardSize = inFS.nextInt();

Check if the inFS (File) has next input as int or not.

Correct it with

if(inFS.hasNextInt())
boardSize = inFS.nextInt();

答案2

得分: 0

抱歉,但我无法复制您提供的代码中的问题。

我复制了您的代码,添加了导入、类定义和虚拟的 writeLogFile 方法,然后复制了您的示例文本文件并猜测了您如何运行它。请看下面的输出。

在发布问题之前自己进行这个实验将非常有帮助,因为这样您可以检查您的问题是否真正捕获了问题,并具有可重现的工作步骤:

$ cat foo.txt
31
3 19
5 3
11 15
20 9
17 -13
19 -12
21 -12
27 -26

$ javac Foo.java && java Foo
Enter gameboard data input filename:
foo.txt
Gameboard setup with 30 squares
  4 squares have a chute
  4 squares have a ladder

Log file dummy.txt successfully created

$

以下是 Foo.java 的代码:

import java.util.*;
import java.io.*;

class Foo {
   public static void main(String[] args) throws IOException {
      int[] boardGame = null;
      Scanner scnr = new Scanner(System.in);
      String fileName;
      String LogFile;
      
      try {
      System.out.println("Enter gameboard data input filename:");
      fileName = scnr.next(); 
     
      boardGame = createGameboard(fileName); //hangup appears to be here and throws Exception
      System.out.println();
      LogFile = writeLogFile(fileName, boardGame);
      if(LogFile == "null") {
      throw new Exception("Program will continue without a log file.");
      }
      else {
         System.out.println("Log file " + LogFile + " successfully created");
      }
      System.out.println();
      } catch (IOException excpt) {
         System.out.println(excpt.getMessage());
         System.out.println("Cannot play without gameboard, so program exiting");
      } catch (Exception excpt) {                 //which is caught here and displays the word null
         System.out.println(excpt.getMessage()); 
      }
      
   }

   static String writeLogFile(String x, int[] y) {
     return "dummy.txt";
   }
   
   public static int[] createGameboard(String nameFile) throws IOException {
      int[] gameBoard = null;
      File gameFile = new File(nameFile);
      Scanner inFS = new Scanner(gameFile);
      int boardSize;
      int numLadders = 0;
      int numChutes = 0;
      int index;
      int value;
      
      boardSize = inFS.nextInt();
      boardSize = boardSize;
      gameBoard = new int[boardSize];
      while(inFS.hasNextInt()) {
         index = inFS.nextInt();
         value = inFS.nextInt();
         gameBoard[index] = value;
         if (value > 0) {
            numLadders++;
         }
         else if(value < 0) {
            numChutes++;
         }
         else {
         }        
      }
      
      System.out.println("Gameboard setup with " + (boardSize - 1) + " squares");
      System.out.println("  " + numChutes + " squares have a chute");
      System.out.println("  " + numLadders + " squares have a ladder");
           
      return gameBoard;
      
   }
}
英文:

Sorry about the non-answer, but I am unable to reproduce your problem with the code you have provided.

I copied your code, added imports, a class definition, and a dummy writeLogFile method, then copied your sample text file and guessed at how you ran it. See the output below.

It would be really helpful if you did this experiment yourself before posting, because that way you can check that your question actually captures the problem, and has working steps to reproduce:

$ cat foo.txt
31
3 19
5 3
11 15
20 9
17 -13
19 -12
21 -12
27 -26
$ javac Foo.java &amp;&amp; java Foo
Enter gameboard data input filename:
foo.txt
Gameboard setup with 30 squares
4 squares have a chute
4 squares have a ladder
Log file dummy.txt successfully created
$

Here's Foo.java:

import java.util.*;
import java.io.*;
class Foo {
public static void main(String[] args) throws IOException {
int[] boardGame = null;
Scanner scnr = new Scanner(System.in);
String fileName;
String LogFile;
try {
System.out.println(&quot;Enter gameboard data input filename:&quot;);
fileName = scnr.next(); 
boardGame = createGameboard(fileName); //hangup appears to be here and throws Exception
System.out.println();
LogFile = writeLogFile(fileName, boardGame);
if(LogFile == &quot;null&quot;) {
throw new Exception(&quot;Program will continue without a log file.&quot;);
}
else {
System.out.println(&quot;Log file &quot; + LogFile + &quot; successfully created&quot;);
}
System.out.println();
} catch (IOException excpt) {
System.out.println(excpt.getMessage());
System.out.println(&quot;Cannot play without gameboard, so program exiting&quot;);
} catch (Exception excpt) {                 //which is caught here and displays the word null
System.out.println(excpt.getMessage()); 
}
}
static String writeLogFile(String x, int[] y) {
return &quot;dummy.txt&quot;;
}
public static int[] createGameboard(String nameFile) throws IOException {
int[] gameBoard = null;
File gameFile = new File(nameFile);
Scanner inFS = new Scanner(gameFile);
int boardSize;
int numLadders = 0;
int numChutes = 0;
int index;
int value;
boardSize = inFS.nextInt();
boardSize = boardSize;
gameBoard = new int[boardSize];
while(inFS.hasNextInt()) {
index = inFS.nextInt();
value = inFS.nextInt();
gameBoard[index] = value;
if (value &gt; 0) {
numLadders++;
}
else if(value &lt; 0) {
numChutes++;
}
else {
}        
}
System.out.println(&quot;Gameboard setup with &quot; + (boardSize - 1) + &quot; squares&quot;);
System.out.println(&quot;  &quot; + numChutes + &quot; squares have a chute&quot;);
System.out.println(&quot;  &quot; + numLadders + &quot; squares have a ladder&quot;);
return gameBoard;
}
}

huangapple
  • 本文由 发表于 2020年8月5日 00:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63251244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定