Image

Imagelefeudans wrote in Imagejava_dev

Alrighty, if anyone can point out what I'm doing wrong or make suggestions, I'd greatly appreciate it.
I've never programmed before (and dove into a intro-Java class at school), so seemingly simple tasks are a bit obscure to me.

In this code, I'm *attempting* to take lines from a text file, and seperate each character of the lines into elements of a two-dimensional array.

So if the text file looks like this:

x.3.4.x.
.x.x.x.x
x.xwx.x.
.x.x.xrx
x.xwx.x.
.xrx.0.x
5.6.7.8.
.w.x.x.x

then I want the array set up so that:

char[1][8] will be a "."
char[2][6] will be an "x"
char[3][1] will be a "x"
char[4][7] will be an "r"
... et cetera ...

import java.io.*;
import java.util.*;
import java.lang.ArrayIndexOutOfBoundsException;

public class ScrapQuad {
	
	public static void main(String[] args) throws IOException {

		File checkerBoardFile = new File("test.txt");
		
		java.io.BufferedReader inFile = new java.io.BufferedReader(
		new java.io.FileReader( checkerBoardFile ) );
	
		final int COLS = 8;
		final int ROWS = 8;
				
		char[][] checkersArray = new char[ROWS][COLS];
		
		String line = inFile.readLine();
		
		int i = 0;
		int j = 0;
		int m = 0;
		
		for (; i < ROWS; i++) {
		
			System.out.println(i);
			while (j < COLS) {
			
				while (m < COLS) {
				
					char l = line.charAt(m);
					System.out.print(l);
					
					checkersArray[i][j] = l;
					m++;
					j++;
					
					
				}
			}
			
			j=0;
			m=0;
			
			line = inFile.readLine();		 
			
		}		
	}
}


Background info, for more insight:
The assignment this is for deals with checkersboards. You specify a .txt file ("containing a board"), and the program must search for RED pieces. Once it finds a red piece it must look for a white piece (above it) to jump over and an empty space to land in. This is done as many times as possible until no additional moves can be made. Then the program has to scan to see if any white pieces remain; if no, then red "wins;" if yes, vice versa. My first step was putting the character representations of the board into a 2D array, but I'm having trouble just doing that. Any suggestions on how to implement this?

The problems:
1.) The program doesn't print the first line (line #0) of the text file (completely omits the "x.3.4.x.")
2.) I get a NullPointerException for line 42 (I guess it tries to go up to 8, and because there is no character at 8, it crashes?)