有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不能有大于10*10个字符的文件

是什么使得它只能输入10*10个文本文件

package game;

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

public class Level {

static public void main(String[] args) throws IOException {
    File f = new File("Data1.txt");
    int[][] m = Map(f);
    for (int x = 0; x < m.length; x++) {
        for (int y = 0; y < m[x].length; y++) {
            System.out.print(m[x][y]);
        }
        System.out.println();
    }
}

public static int[][] Map(File f) throws IOException {

    ArrayList line = new ArrayList();
    BufferedReader br = new BufferedReader(new FileReader(f));
    String s = null;
    while ((s = br.readLine()) != null) {
        line.add(s);
    }
    int[][] map = new int[line.size()][];
    for (int i = 0; i < map.length; i++) {
        s = (String) line.get(i);
        StringTokenizer st = new StringTokenizer(s, " ");
        int[] arr = new int[st.countTokens()];
        for (int j = 0; j < arr.length; j++) {
            arr[j] = Integer.parseInt(st.nextToken());
        }
        map[i] = arr;
    }
    return map;
}
}

如果我放入一个文本文件 10*10个或更少字符有效 否则它会产生一个numberformatexception

固定的

包装游戏

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

public class Level {

    static public void main(String[] args) throws IOException {
        File f = new File("Data1.txt");
        int[][] m = Map(f);
        for (int x = 0; x < m.length; x++) {
            for (int y = 0; y < m[x].length; y++) {
                System.out.print(m[x][y]);
            }
            System.out.println();
        }
    }

    public static int[][] Map(File f) throws IOException {

        ArrayList line = new ArrayList();
        BufferedReader br = new BufferedReader(new FileReader(f));
        String s = null;
        while ((s = br.readLine()) != null) {
            line.add(s);
        }
        int[][] map = new int[line.size()][];
        for (int i = 0; i < map.length; i++) {
            s = (String) line.get(i);
            char[] m = s.toCharArray();
            String[] n = new String[m.length];
            for (int t = 0; t<m.length;t++)
            {
                n[t] = ""+m[t];
            }

            int[] arr = new int[m.length];
            for (int j = 0; j < arr.length; j++) {
                arr[j] = Integer.parseInt(n[j]);
            }
            map[i] = arr;
        }
        return map;
    }
}

共 (2) 个答案

  1. # 1 楼答案

    与注释中的注释相反,只要有足够的空间,您的程序似乎可以处理大文件和长行

    我认为您的问题实际上是,每当文本文件具有超过10个字符的标记时,它就会抛出NumberFormatException

    这是因为Integer.MAX_INT是2147483647,当作为字符串写入时,它有10个字符,而且Integer.parseInt无法处理更多的数字

    您在空间上进行拆分,希望所有内容都解析为整数,而您的一些数字对于Java的整数数据类型来说太大了

  2. # 2 楼答案

    Int的最大值为:2147483647或2147483647,不带逗号。这是10个字符。尝试将11个字符的字符串解析为int将导致一个超出int的最大值的数字,因此出现NumberFormatException