Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help making a method inloving a 2d array and file IO

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

     
    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);
     
     
     
     
     
     
    	}
     
     
     
    }
    Last edited by Norm; February 12th, 2013 at 08:46 PM. Reason: removed spaces in code tag


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help making a method inloving a 2d array and file IO

    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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. trouble with making a simple construct for an array of double type
    By Abadude in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 29th, 2012, 05:05 PM
  2. Making a method to move blocks in a grid
    By geforce in forum What's Wrong With My Code?
    Replies: 60
    Last Post: May 22nd, 2012, 07:00 AM
  3. [SOLVED] Reading file and adding it to an array through a method
    By PeskyToaster in forum Collections and Generics
    Replies: 20
    Last Post: November 10th, 2011, 01:16 AM
  4. Help with making the user input to store in file
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 06:52 AM
  5. Making computer independent file path
    By Javabeginner in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 2nd, 2010, 03:56 PM

Tags for this Thread