为什么这个“其他”声明不起作用?

时间:2013-05-03 05:31:06

标签: java

编辑

这是整个计划。我将char包装在一个Character包装器中以使用.equals()并修复索引问题,并用文字替换常量。程序编译并运行正常,告诉您符号是否匹配,但是当有不匹配的符号时仍然会跳过“else”语句:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;


public class Exercise22_11 {

/**
 * 
 */
public static void main(String[] args) {

        /**for command line argument*/

        if(args.length == 0) {
            System.out.println("You didn't enter an argument dummy!");
            System.exit(0);
        }


        String fileString = "";

        /**Open the file and read it*/

        try{    
            File myOutFile = new File(args[0]);
            Scanner input = new Scanner(myOutFile);
            while (input.hasNext()){
        fileString += input.next();
            }

        }//try

        catch (FileNotFoundException e){
      System.out.println("Sorry that file is not found " + e);
        }//catch

        /**Build the list of characters from file*/

        char[] charArray = new char[fileString.length()];

        for (int i = 0; i < fileString.length(); i++) {
            charArray[i] = fileString.charAt(i);
        }//for building charArray

        /**Create a stack to manipulate grouping symbols*/

        Stack<Character> stack = new Stack<Character>();

        /**Pushes grouping symbols into stack and pops them when they correspond*/

        for (int i = 0; i < charArray.length; i++) {
            Character temp = new Character(charArray[i]);

            if (temp.equals('{') || temp.equals('(') || temp.equals('[')) {
                stack.push(temp);
            }   else if (temp.equals('}') || temp.equals(')') || temp.equals(']')) {
                if (temp.equals('}') && stack.peek().equals('{')){
                    stack.pop();
                }   else if (temp.equals(')') && stack.peek().equals('(')) {
                    stack.pop();
                }   else if (temp.equals(']') && stack.peek().equals('[')) {
                    stack.pop();
                }   else {
                    System.out.println("There is a mistake at index: " + i);
                    System.out.println("\nHere's the code (the error is in the middle):\n");
                    for(int j = i - 20; j <= i + 20; j++) {
                        System.out.print(charArray[j]);
                    }
                    System.out.println("\n");
                }
            }

        }//for

        /**Inform user of result*/

        if (stack.isEmpty()) {
            System.out.println("Congratulations!  Your grouping symbols are matched!");
        }
        else {
            System.out.println("I'm sorry.  Please fix your program.");
        }



}//main



}//class

当出现错误时,在运行时跳过最后的“else”语句。

分配是使用列表或集合编写程序,以检查程序中的分组符号是否重叠。该文件应作为命令行参数输入。

这是我问教授的问题(还没有回答):

  1. 为什么我不能创建扫描程序来读取.txt(或.java)文件中的每个字符?我尝试使用“”(没有空格)的分隔符来避免使用RIDICULOUS String to Char []来操作stack()。

  2. friggin'“else”声明是什么?

  3. 谢谢!

1 个答案:

答案 0 :(得分:0)

如果此print语句用于调试,则不应将其放在else内。你希望你的错误进入其中一个要修复(或弹出)的其他内容,然后打印发现错误的 。摆脱其他声明。做到这一点

}   else if (temp == SIX && stack.peek().equals(FIVE)) {
                    stack.pop();
                }   
    System.out.println("There is a mistake at index: " + charArray[i]); //it will work now
相关问题