# Problem with word puzzle program

Hi All,
I need a help with my program. My program is about finding a word in 2D array horizontal, vertical, or diagonal in any direction but I have some problems with finding the word in the diagonal direction. I don't know what I did wrong.Can anyone help me to solve this problem ?
my code is below:
``````// my code in the main class
for (int i=0;i<wordlist.length;i++){
str1=wordlist[i];
str1=str1.toUpperCase();
for( int j=0;j<puzzleinput.length;j++){
for(int l=0;l<puzzleinput.length;l++){
if (str1.charAt(0)==puzzleinput[j][l]){
if (HorizForward(str1,j,l)){
direction="hf";
SolutionArray(str1,j,l,direction);}
else if (HorizBackward(str1,j,l)){
direction="hb";
SolutionArray(str1,j,l,direction);}
else if (VerticalUp(str1,j,l)){
direction="vu";
SolutionArray(str1,j,l,direction);}
else if (VerticalDown(str1,j,l)){
direction="vd";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpRight(str1,j,l)==true){
direction="dur";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpLeft(str1,j,l)==true){
direction="dul";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownRight(str1,j,l)==true){
direction="ddr";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownLeft(str1,j,l)==true){
direction="ddl";
SolutionArray(str1,j,l,direction);}
}}
}

// this is all the functions that I have used

public static boolean HorizForward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j>49)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j+1;
else break;
}
if (k==str.length()) return true;
else
return false;
}

public static boolean HorizBackward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalUp(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i<0)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalDown(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i>49)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i+1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalUpRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i-1;j=j+1; }
else break;}
if (k==str.length()) return true;
else return false;
}

public static boolean DiagonalUpLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i--;j--; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalDownRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i++;j++; }
else
break;}
if (k==str.length()) return true;
else
return false;
}
public static boolean DiagonalDownLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i+1;j=j-1; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static void SolutionArray(String str,int i, int j, String d){

switch (d){
case "hf":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j+1;
} break;
case "hb":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j-1;
} break;
case "print":
for(int k=0;k<solutionarray.length;k++)
System.out.println(solutionarray[k]);
case "vu":
for(int k=0;k<str.length()-1;k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;
}
case "vd":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;
}
case "dur":
for(int k=str.length()-1;k>=0;k--){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;j=j+1;
}
case "dul":
for(int k=str.length()-1;k>=0;k--){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j+1;
}
case "ddr":
for(int k=str.length()-1;k>=0;k--){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j+1;
}
case "ddl":
if(i>49||j>49)break;
for(int k=str.length()-1;k>=0;k--){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j-1;
}

}

}
``````
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT Business Systems Analyst / Software DeveloperCommented:
Are you able to provide complete code that we can run and test/debug this ourselves? Also, can you elaborate on "I have some problems finding the word in the diagonal direction"? What problems? What is happening that shouldn't be or what is NOT happening, etc?

Otherwise, some things that I can see straight away.. The first two "case" statements in the switch block have "break" after the loop, but none of the others do. I bet this could be part of your issue, along with the fact that the case for "vu" only loops over "str.length() - 1" characters rather than all of them. Also, why do the loops for some cases go backwards (k--) and some forwards (k++). There should be no reason why they all can't just go forwards.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
This is my entire program
``````/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package wordpuzzle;
import java.util.*;
import java.io.*;

/**
*
*/
public class WordPuzzle {
static char solutionarray[][]=new char[50][50];
static char puzzleinput[][]= new char[50][50];

public static boolean HorizForward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j>49)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j+1;
else break;
}
if (k==str.length()) return true;
else
return false;
}

public static boolean HorizBackward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalUp(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i<0)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalDown(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i>49)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i+1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalUpRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i-1;j=j+1; }
else break;}
if (k==str.length()) return true;
else return false;
}

public static boolean DiagonalUpLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i--;j--; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalDownRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i++;j++; }
else
break;}
if (k==str.length()) return true;
else
return false;
}
public static boolean DiagonalDownLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i+1;j=j-1; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static void SolutionArray(String str,int i, int j, String d){

switch (d){
case "hf":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j+1;
} break;
case "hb":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j-1;
} break;
case "print":
for(int k=0;k<solutionarray.length;k++)
System.out.println(solutionarray[k]);
case "vu":
for(int k=0;k<str.length()-1;k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;
}
case "vd":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;
}
case "dur":
for(int k=str.length()-1;k>=0;k--){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;j=j+1;
}
case "dul":
for(int k=str.length()-1;k>=0;k--){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j+1;
}
case "ddr":
for(int k=str.length()-1;k>=0;k--){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j+1;
}
case "ddl":
if(i>49||j>49)break;
for(int k=str.length()-1;k>=0;k--){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j-1;
}

}

}
public static void main(String[] args) {
// TODO code application logic here

String [] wordlist=new String [20];
try {
Scanner scan=new Scanner(new File("puzzleinput.txt"));
String str="";
for(int i=0;i<puzzleinput.length;i++){
str=scan.next();
puzzleinput[i]=str.toCharArray();

}

} catch(FileNotFoundException e) {
System.out.println("Couldn't open file \"puzzleinput.txt\"");
}
try {
Scanner scan=new Scanner(new File("wordlist.txt"));
String str="";
for(int i=0;i<wordlist.length;i++){
str=scan.next();
wordlist[i]=str;

}

} catch(FileNotFoundException e) {
System.out.println("Couldn't open file \"wordlist.txt\"");
}
String str1="";
String direction="";

for (int i=0;i<wordlist.length;i++){
str1=wordlist[i];
str1=str1.toUpperCase();
for( int j=0;j<puzzleinput.length;j++){
for(int l=0;l<puzzleinput.length;l++){
if (str1.charAt(0)==puzzleinput[j][l]){
if (HorizForward(str1,j,l)){
direction="hf";
SolutionArray(str1,j,l,direction);}
else if (HorizBackward(str1,j,l)){
direction="hb";
SolutionArray(str1,j,l,direction);}
else if (VerticalUp(str1,j,l)){
direction="vu";
SolutionArray(str1,j,l,direction);}
else if (VerticalDown(str1,j,l)){
direction="vd";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpRight(str1,j,l)==true){
direction="dur";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpLeft(str1,j,l)==true){
direction="dul";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownRight(str1,j,l)==true){
direction="ddr";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownLeft(str1,j,l)==true){
direction="ddl";
SolutionArray(str1,j,l,direction);}
}}
}

}

for (int i=0;i<puzzleinput.length;i++)
System.out.println(puzzleinput[i]);
for (int i=0;i<wordlist.length;i++)
System.out.println(wordlist[i]);
String str2="";
int a=50,b=50;

SolutionArray(str2,a,b,"print");

}

}

``````
0
Author Commented:
This is my new code after adding break but still the output for the diagonal direction is wrong
``````/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package wordpuzzle;
import java.util.*;
import java.io.*;

/**
*
*/
public class WordPuzzle {
static char solutionarray[][]=new char[50][50];
static char puzzleinput[][]= new char[50][50];

public static boolean HorizForward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j>49)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j+1;
else break;
}
if (k==str.length()) return true;
else
return false;
}

public static boolean HorizBackward(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0)break;
if (str.charAt(k)==puzzleinput[i][j])
j=j-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalUp(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i<0)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i-1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean VerticalDown(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(i>49)break;
if (str.charAt(k)==puzzleinput[i][j])
i=i+1;
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalUpRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i-1;j=j+1; }
else break;}
if (k==str.length()) return true;
else return false;
}

public static boolean DiagonalUpLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i--;j--; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static boolean DiagonalDownRight(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i++;j++; }
else
break;}
if (k==str.length()) return true;
else
return false;
}
public static boolean DiagonalDownLeft(String str,int i, int j){
int k;
for (k=0;k<str.length();k++){

if(j<0 ||j>49 || i>49||i<0)break;
if (str.charAt(k)==puzzleinput[i][j]){
i=i+1;j=j-1; }
else
break;}
if (k==str.length()) return true;
else
return false;
}

public static void SolutionArray(String str,int i, int j, String d){

switch (d){
case "hf":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j+1;
} break;
case "hb":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
j=j-1;
} break;
case "print":
for(int k=0;k<solutionarray.length;k++)
System.out.println(solutionarray[k]);break;
case "vu":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;
}break;
case "vd":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;
}break;
case "dur":
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i-1;j=j+1;
}break;
case "dul":
for(int k=0;k<str.length();k++){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j-1;
}break;
case "ddr":
for(int k=0;k<str.length();k++){
if(i>49||j>49)break;
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j+1;
}break;
case "ddl":
if(i>49||j>49)break;
for(int k=0;k<str.length();k++){
solutionarray[i][j]=puzzleinput[i][j];
i=i+1;j=j-1;
}break;

}

}
public static void main(String[] args) {
// TODO code application logic here

String [] wordlist=new String [20];
try {
Scanner scan=new Scanner(new File("puzzleinput.txt"));
String str="";
for(int i=0;i<puzzleinput.length;i++){
str=scan.next();
puzzleinput[i]=str.toCharArray();

}

} catch(FileNotFoundException e) {
System.out.println("Couldn't open file \"puzzleinput.txt\"");
}
try {
Scanner scan=new Scanner(new File("wordlist.txt"));
String str="";
for(int i=0;i<wordlist.length;i++){
str=scan.next();
wordlist[i]=str;

}

} catch(FileNotFoundException e) {
System.out.println("Couldn't open file \"wordlist.txt\"");
}
String str1="";
String direction="";

for (int i=0;i<wordlist.length;i++){
str1=wordlist[i];
str1=str1.toUpperCase();
for( int j=0;j<puzzleinput.length;j++){
for(int l=0;l<puzzleinput.length;l++){
if (str1.charAt(0)==puzzleinput[j][l]){
if (HorizForward(str1,j,l)){
direction="hf";
SolutionArray(str1,j,l,direction);}
else if (HorizBackward(str1,j,l)){
direction="hb";
SolutionArray(str1,j,l,direction);}
else if (VerticalUp(str1,j,l)){
direction="vu";
SolutionArray(str1,j,l,direction);}
else if (VerticalDown(str1,j,l)){
direction="vd";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpRight(str1,j,l)==true){
direction="dur";
SolutionArray(str1,j,l,direction);}
else if (DiagonalUpLeft(str1,j,l)==true){
direction="dul";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownRight(str1,j,l)==true){
direction="ddr";
SolutionArray(str1,j,l,direction);}
else if (DiagonalDownLeft(str1,j,l)==true){
direction="ddl";
SolutionArray(str1,j,l,direction);}
}}
}

}

for (int i=0;i<puzzleinput.length;i++)
System.out.println(puzzleinput[i]);
for (int i=0;i<wordlist.length;i++)
System.out.println(wordlist[i]);
String str2="";
int a=50,b=50;

SolutionArray(str2,a,b,"print");

}

}

``````
0
Author Commented:
Thanks mccarl I followed your suggestions and my program worked fine. Thanks a lot.
0
IT Business Systems Analyst / Software DeveloperCommented:
You're welcome! :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.