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 9 of 9

Thread: My loop compiles and seems correct, but gives wrong output.

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

    Default My loop compiles and seems correct, but gives wrong output.

    Hey, guys I have a checkerboard logic assignment here. my goal was to print out how many possible jumps red checkers can jump black checkers given random checker arrangements given by the infile. I have this running a working perfectly. For extra credit, we are asked to return the output of all red checkers that are capable of committing a double jump. My loops like right to me, but when I put in a certain infile i have imported called "test1.txt" it returns that there are two double jumps possible. I dont see these possible jumps. what is wrong with my loops in doubleJump()? the txt file of test 1 is easy to import it is just this: R_R___R_
    _B_R_R_R
    __R___B_
    ________
    ____R_R_
    _B_R_B_B
    B_B_R___
    ___B_B_B

    Here is my code:

    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
    	 * Initializes a 2d array, a single array, and four integer variables.
    	 */
    		static char [][] checkerboard = new char [8] [8];
    		static Checker [] reds = new Checker[12];
    		static int redcounter;
    		static int possibleJumps;
    		static int doubleJumps;
    		static Checker[] dubs = new Checker[12];
    		static int dubscounter;
     
    		/**
    		 * reads in the infile and stores the 64 characters into the 8x8 2d array.
    		 * @param infile
    		 */
    		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);
     
    				}
    			}
    			//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
    			//STORE IT INTO THE 2-D ARRAY FOR TASK #2
    			infile.close();
     
    		}
     
    		/**
    		 * creates the empty 8x8 array
    		 */
    	public Checkerboard()
    	{
    		checkerboard = new char [8] [8];
     
    	}
     
    	/**
    	 * the method counts the possible jumps black can jump red given the conditions. it then stores the value in recounter.
    	 * @return returns the counter and stores the counter value in possibleJums
    	 */
    	public static int countJumps()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker red = new Checker(i,j); 
    					reds[redcounter] = red;
    					redcounter++;
    				}
     
    			}
    		}
     
    		for(int i = 0; i < reds.length;i++)
    		{
    			if (reds[i] != null)
    			{	
    				int row = reds[i].getRow();
    				int col = reds[i].getCol();
     
    				if(col>1 && row<6)
    				{
     
    					if(checkerboard[row+1][col-1]=='B'&& checkerboard[row+2][col-2] == '_')
     
    					{
    						possibleJumps++;
    					}
    				}
    				if(col<6 && row<6)	
    				{
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
    					{
    						possibleJumps++;
    					}
    				}
    			}
    		}
    		return possibleJumps;
    	}
     
     
    	public static int countDouble()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker dub = new Checker(i,j); 
    					dubs[dubscounter] = dub;
    					dubscounter++;
    				}
     
    			}
    		}
    		for(int i = 0; i < dubs.length;i++)
    		{
    			if (dubs[i] != null)
    			{	
    				int row = dubs[i].getRow();
    				int col = dubs[i].getCol();
     
    				if(col<6 && row<4)
    				{
    					//check for right and left double jump
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
     
    					{
    						if(checkerboard[row+3][col+1]=='B'&&checkerboard[row+4][col]=='_')
    						doubleJumps++;
    					}
     
    				}
    				if( col < 4 && row < 4)
    				{
    					//check for right and right double jump
    					if(checkerboard[row+1][col+1]=='B'&&checkerboard[row+2][col+2]=='_')
    					{
    						if(checkerboard[row+3][col+3]=='B'&&checkerboard[row+4][col+4]=='_')
    							doubleJumps++;
    					}
    				}
     
    				if( col>3 && row<4)
    				//left left double jump
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-3]=='B'&&checkerboard[row+4][col-4]=='_')
    							doubleJumps++;
     
    					 }
     
    				}
     
    				if(col>1 &&row<4)
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-1]=='B'&& checkerboard[row+4][col]=='_');
    							doubleJumps++;
    					}
     
    				}
    			}
    		}
    		return doubleJumps;
    	}
     
     
     
     
    	/**
    	 * Main method creates a scanner and asks the user for the infile name.
    	 * creates the new obeject board and prints the array aswell as a return statement of possible jumps.
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException
     
    	{
    		// TODO Auto-generated method stub
     
    		Scanner in = new Scanner(System.in);
    		System.out.print("Please enter the file configuration name: " );
    		String input = in.nextLine();
    		Checkerboard board = new Checkerboard();
    		File file  = new File(input);
    		Scanner infile = new Scanner(file);
    		board.readFile(infile);
    		for(int i = 0; i<8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				System.out.print(checkerboard[i][j]);
    			}
    			System.out.println();
    		}	
    		countJumps();
    		System.out.println("There are "+possibleJumps + " possible moves for Red that will result in at least one checker being captured.");
    		System.out.println();
    		countDouble();
    		System.out.println("There are "+doubleJumps +" possible double jump moves for Red.");
    	} 
    }


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

    Default My loops are working, but not giving right output.

    Hey guys, I am working on this checkerboard assignment. I have everything working correctly that I need, but for extra credit i need to print out how many double jumps red checkers can make given a random in file. My loops look right to me, but on the infile "test1.txt which is simply this txt file: it reads that red has two double jumps and i don't believe there is any. whats wrong with my loops? here is my code. I'm using eclipse by the way. Thanks.
    R_R___R_
    _B_R_R_R
    __R___B_
    ________
    ____R_R_
    _B_R_B_B
    B_B_R___
    ___B_B_B

    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
    	 * Initializes a 2d array, a single array, and four integer variables.
    	 */
    		static char [][] checkerboard = new char [8] [8];
    		static Checker [] reds = new Checker[12];
    		static int redcounter;
    		static int possibleJumps;
    		static int doubleJumps;
    		static Checker[] dubs = new Checker[12];
    		static int dubscounter;
     
    		/**
    		 * reads in the infile and stores the 64 characters into the 8x8 2d array.
    		 * @param infile
    		 */
    		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);
     
    				}
    			}
    			//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
    			//STORE IT INTO THE 2-D ARRAY FOR TASK #2
    			infile.close();
     
    		}
     
    		/**
    		 * creates the empty 8x8 array
    		 */
    	public Checkerboard()
    	{
    		checkerboard = new char [8] [8];
     
    	}
     
    	/**
    	 * the method counts the possible jumps black can jump red given the conditions. it then stores the value in recounter.
    	 * @return returns the counter and stores the counter value in possibleJums
    	 */
    	public static int countJumps()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker red = new Checker(i,j); 
    					reds[redcounter] = red;
    					redcounter++;
    				}
     
    			}
    		}
     
    		for(int i = 0; i < reds.length;i++)
    		{
    			if (reds[i] != null)
    			{	
    				int row = reds[i].getRow();
    				int col = reds[i].getCol();
     
    				if(col>1 && row<6)
    				{
     
    					if(checkerboard[row+1][col-1]=='B'&& checkerboard[row+2][col-2] == '_')
     
    					{
    						possibleJumps++;
    					}
    				}
    				if(col<6 && row<6)	
    				{
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
    					{
    						possibleJumps++;
    					}
    				}
    			}
    		}
    		return possibleJumps;
    	}
     
     
    	public static int countDouble()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker dub = new Checker(i,j); 
    					dubs[dubscounter] = dub;
    					dubscounter++;
    				}
     
    			}
    		}
    		for(int i = 0; i < dubs.length;i++)
    		{
    			if (dubs[i] != null)
    			{	
    				int row = dubs[i].getRow();
    				int col = dubs[i].getCol();
     
    				if(col<6 && row<4)
    				{
    					//check for right and left double jump
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
     
    					{
    						if(checkerboard[row+3][col+1]=='B'&&checkerboard[row+4][col]=='_')
    						doubleJumps++;
    					}
     
    				}
    				if( col < 4 && row < 4)
    				{
    					//check for right and right double jump
    					if(checkerboard[row+1][col+1]=='B'&&checkerboard[row+2][col+2]=='_')
    					{
    						if(checkerboard[row+3][col+3]=='B'&&checkerboard[row+4][col+4]=='_')
    							doubleJumps++;
    					}
    				}
     
    				if( col>3 && row<4)
    				//left left double jump
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-3]=='B'&&checkerboard[row+4][col-4]=='_')
    							doubleJumps++;
     
    					 }
     
    				}
     
    				if(col>1 &&row<4)
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-1]=='B'&& checkerboard[row+4][col]=='_');
    							doubleJumps++;
    					}
     
    				}
    			}
    		}
    		return doubleJumps;
    	}
     
     
     
     
    	/**
    	 * Main method creates a scanner and asks the user for the infile name.
    	 * creates the new obeject board and prints the array aswell as a return statement of possible jumps.
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException
     
    	{
    		// TODO Auto-generated method stub
     
    		Scanner in = new Scanner(System.in);
    		System.out.print("Please enter the file configuration name: " );
    		String input = in.nextLine();
    		Checkerboard board = new Checkerboard();
    		File file  = new File(input);
    		Scanner infile = new Scanner(file);
    		board.readFile(infile);
    		for(int i = 0; i<8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				System.out.print(checkerboard[i][j]);
    			}
    			System.out.println();
    		}	
    		countJumps();
    		System.out.println("There are "+possibleJumps + " possible moves for Red that will result in at least one checker being captured.");
    		System.out.println();
    		countDouble();
    		System.out.println("There are "+doubleJumps +" possible double jump moves for Red.");
    	} 
    }

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

    Default My loop compiles and seems correct, but gives wrong output.

    I have everything right, except for my doublejump method. my loops seems right, my goal is to have the second output print how many possible time a red checker could "doublejump" a black checker. It complies, but when I load a infile named "test1.txt" with the array filled like this :

    _B_R_R_R
    __R___B_
    ________
    ____R_R_
    _B_R_B_B
    B_B_R___
    ___B_B_B

    Why is my output saying two jumps? I dont think they're any!
    Here is my code.
    Thanks.

    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
    	 * Initializes a 2d array, a single array, and four integer variables.
    	 */
    		static char [][] checkerboard = new char [8] [8];
    		static Checker [] reds = new Checker[12];
    		static int redcounter;
    		static int possibleJumps;
    		static int doubleJumps;
    		static Checker[] dubs = new Checker[12];
    		static int dubscounter;
     
    		/**
    		 * reads in the infile and stores the 64 characters into the 8x8 2d array.
    		 * @param infile
    		 */
    		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);
     
    				}
    			}
    			//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
    			//STORE IT INTO THE 2-D ARRAY FOR TASK #2
    			infile.close();
     
    		}
     
    		/**
    		 * creates the empty 8x8 array
    		 */
    	public Checkerboard()
    	{
    		checkerboard = new char [8] [8];
     
    	}
     
    	/**
    	 * the method counts the possible jumps black can jump red given the conditions. it then stores the value in recounter.
    	 * @return returns the counter and stores the counter value in possibleJums
    	 */
    	public static int countJumps()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker red = new Checker(i,j); 
    					reds[redcounter] = red;
    					redcounter++;
    				}
     
    			}
    		}
     
    		for(int i = 0; i < reds.length;i++)
    		{
    			if (reds[i] != null)
    			{	
    				int row = reds[i].getRow();
    				int col = reds[i].getCol();
     
    				if(col>1 && row<6)
    				{
     
    					if(checkerboard[row+1][col-1]=='B'&& checkerboard[row+2][col-2] == '_')
     
    					{
    						possibleJumps++;
    					}
    				}
    				if(col<6 && row<6)	
    				{
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
    					{
    						possibleJumps++;
    					}
    				}
    			}
    		}
    		return possibleJumps;
    	}
     
     
    	public static int countDouble()
    	{
    		for(int i = 0; i < 8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				if(checkerboard[i][j] == 'R')
    				{
    					Checker dub = new Checker(i,j); 
    					dubs[dubscounter] = dub;
    					dubscounter++;
    				}
     
    			}
    		}
    		for(int i = 0; i < dubs.length;i++)
    		{
    			if (dubs[i] != null)
    			{	
    				int row = dubs[i].getRow();
    				int col = dubs[i].getCol();
     
    				if(col<6 && row<4)
    				{
    					//check for right and left double jump
    					if(checkerboard[row+1][col+1]=='B'&& checkerboard[row+2][col+2] == '_')
     
    					{
    						if(checkerboard[row+3][col+1]=='B'&&checkerboard[row+4][col]=='_')
    						doubleJumps++;
    					}
     
    				}
    				if( col < 4 && row < 4)
    				{
    					//check for right and right double jump
    					if(checkerboard[row+1][col+1]=='B'&&checkerboard[row+2][col+2]=='_')
    					{
    						if(checkerboard[row+3][col+3]=='B'&&checkerboard[row+4][col+4]=='_')
    							doubleJumps++;
    					}
    				}
     
    				if( col>3 && row<4)
    				//left left double jump
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-3]=='B'&&checkerboard[row+4][col-4]=='_')
    							doubleJumps++;
     
    					 }
     
    				}
     
    				if(col>1 &&row<4)
    				{
    					if(checkerboard[row+1][col-1]=='B'&&checkerboard[row+2][col-2]=='_')
    					{
    						if(checkerboard[row+3][col-1]=='B'&& checkerboard[row+4][col]=='_');
    							doubleJumps++;
    					}
     
    				}
    			}
    		}
    		return doubleJumps;
    	}
     
     
     
     
    	/**
    	 * Main method creates a scanner and asks the user for the infile name.
    	 * creates the new obeject board and prints the array aswell as a return statement of possible jumps.
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException
     
    	{
    		// TODO Auto-generated method stub
     
    		Scanner in = new Scanner(System.in);
    		System.out.print("Please enter the file configuration name: " );
    		String input = in.nextLine();
    		Checkerboard board = new Checkerboard();
    		File file  = new File(input);
    		Scanner infile = new Scanner(file);
    		board.readFile(infile);
    		for(int i = 0; i<8; i++)
    		{
    			for(int j = 0; j<8; j++)
    			{
    				System.out.print(checkerboard[i][j]);
    			}
    			System.out.println();
    		}	
    		countJumps();
    		System.out.println("There are "+possibleJumps + " possible moves for Red that will result in at least one checker being captured.");
    		System.out.println();
    		countDouble();
    		System.out.println("There are "+doubleJumps +" possible double jump moves for Red.");
    	} 
    }


    --- Update ---

    I also have a checker class that sets and gets my col and row location

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: My loop compiles and seems correct, but gives wrong output.

    .

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

    Default Re: My loop compiles and seems correct, but gives wrong output.

    Have you ever played checkers? A red jumping two black checkers in one motion. for instance this would be considered one double jump.
    ________
    __R_____
    ___B____
    ________
    _____B__
    ________
    ________
    ________

  6. #6
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: My loop compiles and seems correct, but gives wrong output.

    .

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

    Default Re: My loop compiles and seems correct, but gives wrong output.

    Haha its all good. there is 8x8 array of 64 characters. an underscore represents an open space on the checkerboard. R's and B's are not replacing. My loop is asking where there black and open spaces are in reference to each R checker found in my array.

  8. #8
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: My loop compiles and seems correct, but gives wrong output.

    .

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: My loop compiles and seems correct, but gives wrong output.

    The code does not compile because of missing class.

    Try debugging the code by adding println statements at the locations the double jump is detected.
    Print out the values of all the variables used in the if test that returned true. The print outs will show you why the code found the jumps.

    A useful method for displaying 2D arrays:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Not getting the correct output
    By Ashish S in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2012, 02:56 AM
  2. Not getting the correct output
    By KNAYERS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2012, 12:59 PM
  3. Code compiles, Is it logically correct?
    By Eriosblood in forum Object Oriented Programming
    Replies: 1
    Last Post: September 23rd, 2012, 11:34 PM
  4. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 05:36 PM
  5. [SOLVED] No errors, but output not correct? Only one loop performed correctly! Help please :)
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2012, 10:18 AM

Tags for this Thread