Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件读取,加载数组_Java_Multidimensional Array_Bankers Algorithm - Fatal编程技术网

Java 从文件读取,加载数组

Java 从文件读取,加载数组,java,multidimensional-array,bankers-algorithm,Java,Multidimensional Array,Bankers Algorithm,我试图用Java实现Banker算法,但在加载数组时遇到了问题。这是我正在使用的代码 public static void main(String[] args) throws FileNotFoundException { String filename = null; int need[][]; int allocate[][]; int max[][]; int available[][]; int n = 0; int m = 0;

我试图用Java实现Banker算法,但在加载数组时遇到了问题。这是我正在使用的代码

public static void main(String[] args) throws FileNotFoundException {
    String filename = null;
    int need[][];
    int allocate[][];
    int max[][];
    int available[][];
    int n = 0;
    int m = 0;
    int lineCount = 0;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the file name.");
    filename = in.nextLine();

    File textFile = new File(filename);
    Scanner input = new Scanner(textFile);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    n = input.nextInt();
    m = input.nextInt();
    System.out.print("Number of Processes: " + n);
    System.out.print("\nNumber of Processes: " + m);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    String line = input.nextLine();

    while (line != null && lineCount < n) {

        String[] temp = line.split(" ");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                allocate[i][j] = Integer.parseInt(line);
                System.out.println("here");
            }
            line = input.nextLine();
            lineCount++;
        }
    }
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
字符串文件名=null;
int需要[];
int分配[];
int max[][];
int可用[][];
int n=0;
int m=0;
int lineCount=0;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入文件名”);
filename=in.nextLine();
文件textFile=新文件(文件名);
扫描仪输入=新扫描仪(文本文件);
最大值=新整数[n][m];
分配=新整数[n][m];
需要=新整数[n][m];
可用=新整数[1][m];
n=input.nextInt();
m=输入.nextInt();
系统输出打印(“进程数:+n”);
系统输出打印(“\n进程数:“+m”);
最大值=新整数[n][m];
分配=新整数[n][m];
需要=新整数[n][m];
可用=新整数[1][m];
String line=input.nextLine();
while(line!=null&&lineCount
我的示例文件包含此数据

五,

四,

01 2 1 0 0 0 1 3 5 4 0 6 3 2 01 4

01 2 1 7 5 0 2 3 5 6 0 6 5 2 0656

1520

1:0420


所以,在尝试这样做时,我遇到了许多不同的错误。现在我得到一个NumberFormatException:For input string“”错误。非常感谢您提供的任何帮助。

您有一堆非常小的阵列,而且您从不增加它们的大小:

int n = 0;
int m = 0;
...
max = new int[n][m];       // 0x0 array, unused
allocate = new int[n][m];  // 0x0 array, likely the culprit
need = new int[n][m];      // 0x0 array, unused
available = new int[1][m]; // 1x0 array, unused
在这些数组中,只使用了
allocate
,您稍后将在for循环中使用它:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // OutOfBounds is probably here:
            allocate[i][j] = Integer.parseInt(line);
            System.out.println("here");
        }
        line = input.nextLine();
        lineCount++;
    }
for(int i=0;i

另外,您正在运行
Integer.parseInt(line)
,它正试图解析整行。您应该一次只解析一个标记,它应该是
Integer.parseInt(temp[someindex])

您有一堆非常小的数组,并且您从不增加它们的大小:

int n = 0;
int m = 0;
...
max = new int[n][m];       // 0x0 array, unused
allocate = new int[n][m];  // 0x0 array, likely the culprit
need = new int[n][m];      // 0x0 array, unused
available = new int[1][m]; // 1x0 array, unused
在这些数组中,只使用了
allocate
,您稍后将在for循环中使用它:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // OutOfBounds is probably here:
            allocate[i][j] = Integer.parseInt(line);
            System.out.println("here");
        }
        line = input.nextLine();
        lineCount++;
    }
for(int i=0;i

另外,您正在运行
Integer.parseInt(line)
,它正试图解析整行。您应该一次只解析一个标记,它应该是
Integer.parseInt(temp[someindex])

您能解释一下输入文件的格式吗?它似乎不一致(例如,仅最后一行中的冒号)。另外,你能解释一下什么是
n
m
,以及你的代码的直觉吗?如果你能发布ArrayIndexOutOfBoundsException的堆栈跟踪,并说出异常指向的行号,那会很有帮助。你能解释一下输入文件的格式吗?它似乎不一致(例如,仅最后一行中的冒号)。另外,你能解释一下什么是
n
m
,以及你的代码的直觉吗?如果你能发布ArrayIndexOutOfBoundsException的堆栈跟踪,并说出异常指向的行号,那会很有帮助。当然,你是对的。所以我在n和m得到它们的值之后移动赋值语句,现在我得到一个NumberFormatException。文件中有空行。好的,现在您只需要做一点调试——首先打印for循环中的每一行,这样您就知道问题在哪一行。请尝试打印出
line
,而不是打印“here”。numberFormatException可能出现在最后一行,您在那里
1:0
我更改了txt文件(删除了1:0),并且得到了相同的错误。我试着在循环中打印出行,同样的错误。我把它移出了循环,同样的错误。它对输入字符串说:“它与文件中数字之间的空格有关吗?有空格没关系。您正在执行的
String[]temp=line.split(“”)
将字符串在每个空格处拆分。因此,如果您有字符串
“1234”
并在其上进行拆分(“”),您将得到一个数组{“1”、“2”、“3”、“4”},您当然是对的。所以我在n和m得到它们的值之后移动赋值语句,现在我得到一个NumberFormatException。文件中有空行。好的,现在您只需要做一点调试——首先打印for循环中的每一行,这样您就知道问题在哪一行。请尝试打印出
line
,而不是打印“here”。numberFormatException可能出现在最后一行,您在那里
1:0
我更改了txt文件(删除了1:0),并且得到了相同的错误。我试着在循环中打印出行,同样的错误。我把它移出了循环,同样的错误。它对输入字符串说:“它与文件中数字之间的空格有关吗?有空格没关系。您正在执行的
String[]temp=line.split(“”)
将字符串在每个空格处拆分。因此,如果您有字符串
“1234”
并在其上进行拆分(“”),您将得到一个数组{“1”、“2”、“3”、“4”}