I’m doing a crossword program in Java and I’m stuck.
Whenever I try to execute the code like java assign2 > input.txt, nothing happens, it’s like an infinite loop.
Granted that my crossword program is not complete it’s just that if I can’t test it I can’t do anything else, here is my code if you can help.
import java.util.*;
public class A2
{
public static void main(String[] args)
{
String[] a = new String[100];
Scanner scanner = new Scanner(System.in);
String t = scanner.nextLine();
Crossword cw = new Crossword(t);
int count = 0;
System.out.print("1");
for (; !t.equals(""); count++)
{
System.out.print("2");
a[count] = t;
t = scanner.nextLine();
}
for (int j = 0; j < 20; j++)
{
for (int k = 0; k < 20; k++)
System.out.print(cw.crossword[j][k]);
System.out.println("");
}
}
}
/**
The class Crossword knows how to build a crossword layout from
a list of words.
*/
class Crossword
{
public char[][] crossword = new char[20][20];
public Crossword(String first)
{
for (int i = 0; i < first.length(); i++)
crossword[9][i] = first.charAt(i);
}
}
I’m about to give up at this point, so any help would be appreciated.
It seems you’re writing to the same file you’re reading from. You read from
"input.txt"and you call your program withjava assign2 > input.txt(orjava A2...).This means that, as you write to
System.outwhich is redirected toinput.txt, the file gets more lines to read from, and your condition!t.equals("")never becomes false.