Why is my Java Boggle Code not Working?


Why is my Java Boggle Code not Working?
public class coggle
public static void main(String args)
Scanner scan = new Scanner(System.in);
HashSet<String> d = new HashSet<>();
String board = new String[5][5];
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
board[i][j] = scan.next();
scan.nextLine();
String next = "";
while (scan.hasNextLine())
next = scan.nextLine();
if ("done".equals(next))
break;
d.add(next);
boolean visited = new boolean[5][5];
ArrayList<String> s = new ArrayList<>();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
f(i, j, board, visited, "", d, s);
public static void f(int r, int c, String board, boolean visited, String s, HashSet<String> d, ArrayList<String> words)
Basically, I just want the program to print out any words that are found. It never prints any words, however.
It is supposed to work on a 5x5 board, and and the input for the hashset dictionary stops when I type "done".
1 Answer
1
I don't see anything wrong with the algorithm so it may be the input data that is making the algorithm not to print any word. I modified the code to hard code the input data instead of retrieving it from System.in
.
System.in
public class Coogle
public static void main(String args)
HashSet<String> d = new HashSet<>(Arrays.asList("bar", "bars"));
String board = "b", "a", "r", "s", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z";
boolean visited = new boolean[5][5];
ArrayList<String> s = new ArrayList<>();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
f(i, j, board, visited, "", d, s);
public static void f(int r, int c, String board, boolean visited, String s, HashSet<String> d, ArrayList<String> words)
The algorithm is printing bar
and bars
bar
bars
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I'm not sure what is your problem, I got the algorithm to print some words. What are you using as input data?
– f-CJ
38 mins ago