what makes it only be able to input 10*10 text files
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;
}
}
if i put in a text file that is
10*10 or less characters it works
otherwise it comes out with a numberformatexception
fixed
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);
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;
}
}
Contrary to notes in the comments, your program seems to work with large files, and with long lines, as long as there are enough spaces.
I think your issue is actually that whenever the text file has a token with more than 10 characters it throws a NumberFormatException.
That would be because
Integer.MAX_INTis 2147483647, which has 10 characters when written as a String, andInteger.parseIntjust can’t handle more digits than that.You’re splitting on space and expecting everything to parse as an integer, and some of your numbers are too big for Java’s integer data type.