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: Word Seach puzzle

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Word Seach puzzle

    What's wrong with this code? It is supposed to take in an array along with a sum to find. then it searches that array to find consecutive vertical or horizontal numbers that sum up to the sum you are looking for.
    public class FindTheSums {
     
    	public static String arrayToString(int[][] a) {
     
    		String string = "";
            for (int row = 0; row < a.length; row++)
            {
                    for (int col = 0; col < a[0].length; col++)
                    {
                            System.out.print(a[row][col] + " ");
                    }
                    System.out.print("\n");
            }
            return string;
    }
    	public static int[][] horizontalSums(int[][] a, int sumToFind) {
     
    		int currentSum = 0;
    		int goalSum = 0;
    		int[][] outputArray = new int[a.length][a[0].length];
    		for (int row = 0; row < a.length; row++) {
    			for (int col = 0; col < a[row].length; col++) {
    				int currentPosition = col;
    				currentSum = 0;
    				int start = col;
    				while (currentSum < goalSum) {
    					currentSum = a[row][currentPosition] + currentSum;
    					currentPosition++;
    				}
    				if (currentSum == goalSum) {
    					for (int i = start; i < currentPosition; i++) {
    						outputArray[row][i] = a[row][i];
    					}
    				}
    			}
    		}
    		return outputArray;
    	}
     
    	public static int[][] verticalSums(int[][] a, int sumToFind) {
    		int currentSum = 0;
    		int goalSum = 0;
    		int[][] outputArray = new int[a.length][a[0].length];
    		for (int row = 0; row < a.length; row++) {
    			for (int col = 0; col < a[row].length; col++) {
    				int currentPosition = col;
    				currentSum = 0;
    				int start = col;
    				while (currentSum < goalSum) {
    					currentSum = a[row][currentPosition] + currentSum;
    					currentPosition++;
    				}
    				if (currentSum == goalSum) {
    					for (int i = start; i < currentPosition; i++) {
    						outputArray[row][i] = a[row][i];
    					}
    				}
    			}
    		}
    		return outputArray;
    	}
    }
    Tester class.
    public class FindTheSumsTester {
     
    	private static int[][] array1 = { 
    		{3, 2, 1, 1},
    		{2, 5, 6, 2},
    		{1, 2, 9, 8}
    	};
     
    	public static void main(String[] args){
    		arrayToStringTest();
    		horizontalSumsTest();
    		verticalSumsTest();
    	}
     
    	private static void arrayToStringTest() {
    		String methodCallResult, correctAnswer;
    		System.out.println("Testing arrayToString method:");
     
    		methodCallResult = FindTheSums.arrayToString(array1);
    		correctAnswer = "3 2 1 1\n2 5 6 2\n1 2 9 8";
    		if(methodCallResult.equals(correctAnswer)){
    			System.out.println("arrayToString(array1) test passed");
    		}
    		else{
    			System.out.println("arrayToString(array1) test failed");
    		}
    	}
     
    	private static void horizontalSumsTest(){
    		int[][] horizontalSums;
    		String arrayAsString;
    		System.out.println("Testing horizontalSums method:");
    		System.out.println("array1:");
    		arrayAsString = FindTheSums.arrayToString(array1);
    		System.out.println(arrayAsString);
    		System.out.println("horizontalSums(array1, 7):");
    		horizontalSums = FindTheSums.horizontalSums(array1, 7);
    		arrayAsString = FindTheSums.arrayToString(horizontalSums);
    		System.out.println(arrayAsString);
    	}
     
    	private static void verticalSumsTest(){
    		int[][] verticalSums;
    		String arrayAsString;
    		System.out.println("Testing verticalSums method:");
    		System.out.println("array1:");
    		arrayAsString = FindTheSums.arrayToString(array1);
    		System.out.println(arrayAsString);
    		System.out.println("verticalSums(array1, 22):");
    		verticalSums = FindTheSums.verticalSums(array1, 22);
    		arrayAsString = FindTheSums.arrayToString(verticalSums);
    		System.out.println(arrayAsString);
    	}
    }

    Desired output:
    25ae6dce537335dccf8aa5c5ecfb64ca.png3201391f593dba55e71541d15001fa85.jpg

    My output:
    kk.jpg


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

    Default Re: Word Seach puzzle

    Can you explain what is wrong with the current output?

    It is hard to see all the images at the same time for comparison.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  2. Recursive Assistance with a Word Puzzle Game
    By inflames098 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:33 AM
  3. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  4. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM

Tags for this Thread