can someone help me with my code please. I posted my code below. I keep getting
ID: 3704647 • Letter: C
Question
can someone help me with my code please. I posted my code below. I keep getting an error GFG.java:115: error: array required, but String found
boggle[i][j] = line[k]; some help me fix this. and this code is to solve the boggle game. it's suppose to read a dictionary file and a board of 4x4, 5x5, 10x10, and more. Did i do it right to read those files? Can someone help me please. Thank you!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class GFG {
static int row[] = {0,0,1,-1,-1,1,-1,1};
static int col[] = {1,-1,0,0,-1,1,1,-1};
public static void find(String d[],int mat[][])
{
boolean flag = false;
boolean visited[][] = new boolean[3][3];
for(int i=0;i<d.length;i++)
{
for(int k=0;k<3;k++)
{
for(int j=0;j<3;j++)
{
visited[k][j] = false;
}
}
for(int p=0;p<3;p++)
{
for(int q=0;q<3;q++)
{
if(mat[p][q]==d[i].charAt(0))
{
//System.out.println(p+" "+q+" ");
if(findutil(d[i],mat,p,q,0,visited))
{
System.out.println();
flag = true;
break;
}
}
}
if(flag == true)
{
flag = false;
break;
}
}
}
}
public static boolean findutil(String str,int mat[][],int i,int j,int m,boolean visited[][])
{
if(m==str.length()-1)
{
System.out.println(str);
return true;
}
if(m>str.length()-1)
return false;
for(int p=i+0;p<8;p++)
{
if(i+row[p]>=0 && i+row[p]<3 && j+col[p]<3 && j+col[p]>=0 && visited[i+row[p]][j+col[p]] == false &&
mat[i+row[p]][j+col[p]]==str.charAt(m+1))
{
//System.out.print(str.charAt(m+1)+" "+(m+1));
visited[i+row[p]][j+col[p]] = true;
findutil(str, mat, i+row[p], j+col[p], m+1,visited);
}
}
return false;
}
public static void main (String[] args) throws FileNotFoundException {
// I am not sure of how the words are placed in the dictionary.txt.
// Is it comma seperated words OR one word per line.
// below I assume one word per line and store the same in the dictionary[] array.
//creating File instance to reference text file in Java
// provide the correct path to the dictionary.txt file.
File text = new File("C:/temp/dictionary.txt");
//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
String dictionary[];
// assuming the first line will give the number of words in the dictionary.txt
dictionary = new String[scnr.nextInt()];
while(scnr.hasNextLine()){
int i =0 ;
String line = scnr.nextLine();
dictionary[i] = line;
i++;
}
// lets read the 4x4 Board.txt file.
text = new File("C:/temp/Board.txt");
scnr = new Scanner(text);
int boggle[][];
int leng = scnr.nextInt();
boggle = new int[leng][leng];
int i = 0, j=0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
int k = 0;
for( j=0;j<leng;j++){
boggle[i][j] = line[k];
k = k + 2;
}
i++;
}
// String dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"};
//int boggle[][] = {{'G','I','Z'},
// {'U','E','K'},
//{'Q','S','E'}};
// find(dictionary,boggle);
}
}
Explanation / Answer
Please check the modified code below with comments
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GFG {
static int row[] = {0,0,1,-1,-1,1,-1,1};
static int col[] = {1,-1,0,0,-1,1,1,-1};
public static void find(String d[],int mat[][])
{
boolean flag = false;
boolean visited[][] = new boolean[3][3];
for(int i=0;i<d.length;i++)
{
for(int k=0;k<3;k++)
{
for(int j=0;j<3;j++)
{
visited[k][j] = false;
}
}
for(int p=0;p<3;p++)
{
for(int q=0;q<3;q++)
{
if(mat[p][q]==d[i].charAt(0))
{
//System.out.println(p+" "+q+" ");
if(findutil(d[i],mat,p,q,0,visited))
{
System.out.println();
flag = true;
break;
}
}
}
if(flag == true)
{
flag = false;
break;
}
}
}
}
public static boolean findutil(String str,int mat[][],int i,int j,int m,boolean visited[][])
{
if(m==str.length()-1)
{
System.out.println(str);
return true;
}
if(m>str.length()-1)
return false;
for(int p=i+0;p<8;p++)
{
if(i+row[p]>=0 && i+row[p]<3 && j+col[p]<3 && j+col[p]>=0 && visited[i+row[p]][j+col[p]] == false &&
mat[i+row[p]][j+col[p]]==str.charAt(m+1))
{
//System.out.print(str.charAt(m+1)+" "+(m+1));
visited[i+row[p]][j+col[p]] = true;
findutil(str, mat, i+row[p], j+col[p], m+1,visited);
}
}
return false;
}
public static void main (String[] args) throws FileNotFoundException {
// I am not sure of how the words are placed in the dictionary.txt.
// Is it comma seperated words OR one word per line.
// below I assume one word per line and store the same in the dictionary[] array.
//creating File instance to reference text file in Java
// provide the correct path to the dictionary.txt file.
File text = new File("C:/temp/dictionary.txt");
//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
String dictionary[];
// assuming the first line will give the number of words in the dictionary.txt
dictionary = new String[scnr.nextInt()];
while(scnr.hasNextLine()){
int i =0 ;
String line = scnr.nextLine();
dictionary[i] = line;
i++;
}
// lets read the 4x4 Board.txt file.
text = new File("C:/temp/Board.txt");
scnr = new Scanner(text);
int boggle[][];
int leng = scnr.nextInt();
boggle = new int[leng][leng];
int i = 0, j=0;
while(scnr.hasNextLine()){
String line[] = scnr.nextLine().split(" ");//here each line in file contains some number as per your logic and they might be seperated by space so here I am splitting line read here by space here I have changed the type of line to array
int k = 0;
for( j=0;j<leng;j++){
boggle[i][j] = Integer.parseInt(line[k]);//and boggle is int array here converting string content into integer
k = k + 2;
}
i++;
}
// String dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"};
//int boggle[][] = {{'G','I','Z'},
// {'U','E','K'},
//{'Q','S','E'}};
// find(dictionary,boggle);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.