I am writing a code that will import a string of characters from a text file, analyze the string using a stack and determine which "language" the string belongs to based on whether or not it fits certain rules. Why is it in this case:
public static boolean checkL2(File file) throws IOException
{
Stack l2Stack = new Stack();
boolean bStart = false;
char w;
Scanner sc = new Scanner(file).useDelimiter("\s*");
while(sc.hasNext()) //Load input from file onto stack
{
w = sc.next().charAt(0);
if (w == 'A')
{
if (bStart == true)
{
return false;
}
else
{
l2Stack.push('A');
}
}
if (w == 'B')
{
bStart = true;
if (l2Stack.isEmpty() == true)
{
return false;
}
else
{
l2Stack.pop();
}
}
}
sc.close();
if (l2Stack.isEmpty() == true)
{
return true;
}
}
I get an error telling my that I am missing a return statement, but not in this one:
public boolean isEmpty()
{
if (top == -1) {
return true;
}
else
{
return false;
}
}
They look like very similar to me and I can't figure out why one works fine and the other does not. I would like to have the first bit of code return true or false based one whether the string fits the rules A^nB^n.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…