焦炭的最长路径

时间:2018-10-03 23:12:41

标签: java arrays file input boolean

我需要从txt文件名“ input.txt”中读取并在数组中找到A的最长路径,但我遇到了带有null异常的错误,现在突然之间似乎根本没有读取txt文件。我运行了调试器,到目前为止,仍然无法解决问题。到目前为止,这是我针对地图类的代码。

import java.io.*;
import java.util.Scanner;
import java.util.List;

public class Map { //Class will create an array map of chars and find the   
longest path of A
boolean [][] alreadyCounted;
int arrLength; //Used for the length of the array
int numOfArrays = 0; //Used for the columns of the array
char[][] arr; //char array used to create map
int currentLength; //the current length of the path of A
public void readDataFromFile(File file) throws IOException //Will read data from txt file and convert it into char array
{

    File inFile = new File("input.txt"); 
    Scanner scanner = new Scanner(inFile);
    String[] size = scanner.nextLine().split("\\s");
    if (scanner.hasNext()) //if it has another line then # of columns increases
    {
        numOfArrays= numOfArrays + 1;
    }
    for (int i = 0; i < numOfArrays; i++) //if it has another row # of rows increases
    {
        arrLength = arrLength + 1;
    }

    char [][] arr = new char[Integer.parseInt(size[0])][Integer.parseInt(size[1])];
    for (int i = 0; i < arr.length; i++) //fills the array up with the chars
    {
        arr[i] = scanner.nextLine().toCharArray();
    }
    for (int j = 0; j < arr.length; j++)
    {
        for (int k = 0; k < arr[j].length; k++)
        {
            System.out.print(arr[j][k] + " ");
        }
        System.out.println();
    }
    scanner.close();


}       
public int findLongestPathLength() //Creates boolean array to check for the longest path of A
{
    int i, j, x, y;
    int numOfAs = 0; //number of As in the current path
    int longestPath = 0; //the longest path in the file
    alreadyCounted = new boolean[numOfArrays][arrLength]; //boolean array to check if A's were already counted
    for(i = 0; i < numOfArrays; i++) 
    {
        for(j = 0; j < arrLength; j++ ) 
        {
            alreadyCounted[i][j] = false;
        }
    }

    for(i = 0; i < numOfArrays; i++) 
    {
        for(j = 0; j < arrLength; j++ ) 
        {
            if(arr[i][j] == 'A') 
            {
                alreadyCounted[i][j] = true;
                numOfAs = findPathLengthRecursive(i, j) + 1;
            }
        }
    }
   if(numOfAs > longestPath)
    {
        longestPath = numOfAs;
    }


        for(x = 0; x < numOfArrays - 1; x++) 
        {
            for(y = 0; y < arrLength - 1; y++ ) 
            {
                alreadyCounted[x][y] = false;
            }
        }

        currentLength = 0;
        return longestPath;

}
public int findPathLengthRecursive(int i, int j) //Uses recursion to find the longest path of A
{
       if (i < 0 || j < 0 || i >= arrLength ) 
       {
         return 0;
       }


       int result = 1 + Math.max(
           Math.max(
              findPathLengthRecursive(i - 1, j),
              findPathLengthRecursive(i + 1, j)),
           Math.max(
              findPathLengthRecursive(i, j - 1),
              findPathLengthRecursive(i, j + 1)));
       return result;      
     }

}

这是我为测试班准备的东西

import java.io.*;
import java.util.Scanner;
import java.util.List;
public class TestClass 
{
public static void main(String[] args) throws IOException
{
    Map p = new Map();
    p.readDataFromFile();
    p.findLongestPathLength();
    p.findPathLengthRecursive(0, 0);
    System.out.println(p.findLongestPathLength());
}

}

我认为它无法正确读取txt文件

0 个答案:

没有答案