Im trying to read in a .txt file and make a multi dimensional array out of it. I dont understand why the i and j loops arent populating my array. Any pointers much appreciated....
import java.util.*;
import java.io.*;
public class arrayChallenge {
public static void main(String[] args) throws FileNotFoundException{
File input = new File("input.txt");
Scanner scan1 = new Scanner(input);
int width=10, height=10;
char [][] arrayMulti = new char[width][height];
for(int i=0; i<height; i++){
String x = scan1.next();
char[] chars = x.toCharArray();
for(int j=0; j<width; j++){
arrayMulti[i][j]= chars[j];
}
}
for(char [] xy: arrayMulti){
System.out.println(Arrays.toString(xy));
}
}
}
There is nothing wrong with your i and j loops as far as I can see. As long as the provided path to your file is correct and the file has the needed number of lines (>= your width variable) and each line has the needed number of characters (>= your height variable), it should work fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.