2012-10-14 76 views
0

我正在編寫的程序必須讀取已加載的文本文件,並檢查是否所有(,[和{均衡以及忽略了之間的任何內容「。到目前爲止,我已檢查前三個符號是否全部平衡,但我在檢查首次發生的行號時遇到了麻煩,我知道如果下一個符號是「or//我忽略該行或直到我打了下一個」,但我不能確定正確的語法的任何幫助,將不勝感激這是我到目前爲止有:。Java Stack Sort Symbol Balancing

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import java.util.*; 
import java.io.*; 

import javax.swing.filechooser.*; 
import javax.swing.text.BadLocationException; 

public class Sorting extends JFrame implements ActionListener{ 



    private Stack<Character> symbolStack; 



    JTextArea opentextArea; 
    JTextArea placetextArea; 
    JMenuItem open; 
    final JFileChooser fc = new JFileChooser(); 

    public static void main(String [] args) 
    { 
     Sorting gui = new Sorting(); 




    } 




    public Sorting() 
    { 




     JFrame frame = new JFrame(); 
     setLayout(new GridLayout(1,2)); 
     setTitle("Stack Sort"); 

     JMenuBar menuBar = new JMenuBar(); 
     JMenu menuItems = new JMenu("File"); 
     open = new JMenuItem("Open"); 
     open.addActionListener(this); 
     JMenuItem reset = new JMenuItem("Reset"); 


     reset.addActionListener(
       new ActionListener(){ 
        public void actionPerformed(ActionEvent e) 
        { 
         opentextArea.setText(""); 
         placetextArea.setText(""); 
        } 
       } 
      ); 

     menuItems.add(open); 
     menuItems.add(reset); 

     menuBar.add(menuItems); 
     setJMenuBar(menuBar); 
     opentextArea = new JTextArea(); 
     placetextArea = new JTextArea(); 
     add(opentextArea); 
     add(placetextArea); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pack(); 
    setVisible(true); 


    } 




    public void actionPerformed(ActionEvent event) 
    { 

     int returnVal = fc.showOpenDialog(this); 

     if (returnVal == JFileChooser.APPROVE_OPTION) 
     { 

      File file = fc.getSelectedFile(); 
      FileInputStream is = null; 
      try 
      { 
       is = new FileInputStream(file); 
      } 
      catch (FileNotFoundException e) 
      { 
       System.out.println("Exception: " + e.toString()); 
      } 


      byte [] nextChar = new byte[2]; 

      try 
      { 

       int value = is.read(nextChar); 
       int num = 1; 
       opentextArea.append(num + ":"); 


       while (value != -1) 
       { 
        String newString = new String(nextChar); 
        if (nextChar[0] == '\n' || nextChar[0] == '\r') 
        { 
         num++; 
         opentextArea.append(newString + num + ":"); 

        } 
        else 
         opentextArea.append(newString); 


        value = is.read(nextChar); 

       } 


      } 
      catch (IOException e) 
      { 
       System.out.println("Exception: " + e.toString()); 
      } 


      Stack<Character> stack = new Stack<Character>(); 
      String s = opentextArea.getText(); 
      int index = 0; 
      int numline = 1; 
      while(index < s.length()) { 
       char ch = s.charAt(index); 
       if (ch == '\n' || ch == '\r') 
        { 
         numline++; 
         index++; 
        } 
       else 
       if(ch == '{' || ch == '[' || ch == '(') 
        { 
         stack.push(ch); 
         index++; 
        } 
       else { 
        if(stack.empty()) 
        { 
         index++; 
         //placetextArea.append("No balance characters found."); 
         continue; 
        } 

        char ch1 = stack.pop(); 

        if(ch1 == '{' && ch == '}' || ch1 == '[' && ch == ']' || ch1 == '(' && ch == ')') 
        { 
         placetextArea.append(ch1 + " at line " +numline + " matches up with " + ch + " at line " + numline + "\n"); 
        } 
        else 
         if(ch1 == '{' && ch != '}' || ch1 == '[' && ch != ']' || ch1 == '(' && ch != ')') 
         { 
          placetextArea.append("error unmatched " + ch1 + "at line " +numline); 
          break; 
         } 
        } 

       } 






    } 
} 
} 

這是我編輯的代碼:

Stack<Character> stack = new Stack<Character>(); 
     String s = opentextArea.getText(); 
     int index = 0; 
     int numline = 1; 
     while(index < s.length()) { 
      char ch = s.charAt(index); 
      if (ch == '\n' || ch == '\r') 
       { 
        numline++; 
        index++; 
       } 
      else 
      if(ch == '{' || ch == '[' || ch == '(') 
       { 
        stack.push(ch); 
        index++; 
       } 
      else { 
       if(stack.empty()) 
       { 
        index++; 
        //placetextArea.append("No balance characters found."); 
        continue; 
       } 
       // pop an item from stack 
       if(ch == '}' || ch == ']' || ch == ')') 
       { 
       char ch1 = stack.pop(); 
       // check if it's a matching pair 
       if(ch1 == '{' && ch == '}' || ch1 == '[' && ch == ']' || ch1 == '(' && ch == ')') 
       { 
        placetextArea.append(ch1 + " at line " +numline + " matches up with " + ch + " at line " + numline + "\n"); 
       } 
       else 
        if(ch1 == '{' && ch != '}' || ch1 == '[' && ch != ']' || ch1 == '(' && ch != ')') 
        { 
         placetextArea.append("error unmatched " + ch1 + "at line " +numline); 
         break; 

        } 
       } 

      } 






} 
+0

您不必以這種方式編輯您的帖子。如果每個人都這樣做,如何利用資本並分享知識?請取消您上次的編輯。 – Aubin

回答

2

只有遇到),}]中的一個時,才需要從堆棧彈出。然後你檢查一下是否與開幕式相符。你也需要在無與倫比的情況下定義一種行爲:推開或放棄。在EOF你應該檢查堆棧是否爲空。否則,你有不平衡的東西。

+0

是的,你定義了 - 你把彈出的支架放在地板上。要知道爲什麼你需要在EOF中檢查棧是否爲空,請考慮以下文本(它是一個完整的文件內容):'('。 – Serge

+0

抱歉,我的prev聲明關於凍結是錯誤的,但無論如何,你應該刪除現有的'if stack.empty()){...}'當遇到閉合符號時,你需要檢查堆棧是否爲空,因爲它沒有匹配的開放符號也是不平衡的。不要忘記撞上索引(這是凍結的真正原因) – Serge