Help making a method inloving a 2d array and file IO
This is pretty broad, but I'm just looking for a starting point. I have initialized and created an array called checkerboard. I have read in a txt file that has 64 characters that resembles a checkerboard. Eventually I need to make a method that reads in these characters of my txt file and places them in my [8][8] array. I then need to make a loop that determines and prints out how many possible jumps red can make on black. I need to have two classes. I have one named Checkerboard with the main method and another named checkers I haven't yet touched. Again - not looking for anything too specific I just need a bone of how in the world I can begin writing this method. Here is the code I have in my Checker board class so far.
Code java:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Checkerboard
{
/**
* @param args
*/
//public String black = "B";
//public String red = "R";
private char [][] checkerboard = new char [8] [8];
public void readFile(Scanner infile)
{
for(int row = 0; row< checkerboard.length; row++)
{
String value = infile.nextLine();
for(int col = 0 ; col<value.length();col++)
{
checkerboard [row] [col] = value.charAt(col);
}
//System.out.println();
}
//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
//STORE IT INTO THE 2-D ARRAY FOR TASK #2
infile.close();
}
public Checkerboard()
{
checkerboard = new char [8] [8];
}
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
Checkerboard board = new Checkerboard();
File file = new File("test1.txt");
Scanner infile = new Scanner(file);
board.readFile(infile);
//PrintWriter outfile = new PrintWriter(new FileWriter("test1.txt"));
//board.readFile(infile);
//board.printBoard(outfile);
//PrintWriter outfile = new PrintWriter(new FileWriter("precip.txt"));
//board.readFile(infile);
//board.printBoard(outfile);
}
}
Re: Help making a method inloving a 2d array and file IO
Quote:
need to make a method that reads in these characters of my txt file and places them in my [8][8] array
Can you describe what is in the file and what references the pieces have to an x,y location on the board?