Java堆栈排序符号平衡
我正在编写的程序必须读取加载的文本文档,并检查所有(、[、和{是否平衡,以及是否忽略两者之间的任何内容“”到目前为止,我所做的是检查前三个符号是否都是平衡的,但我在检查第一次出现的行号时遇到了问题。我知道我必须写下,如果下一个符号是“或/”,我会忽略这一行,或者直到我点击下一行,但我不确定正确的语法。如有任何帮助,将不胜感激。以下是我目前掌握的信息:Java堆栈排序符号平衡,java,sorting,stack,Java,Sorting,Stack,我正在编写的程序必须读取加载的文本文档,并检查所有(、[、和{是否平衡,以及是否忽略两者之间的任何内容“”到目前为止,我所做的是检查前三个符号是否都是平衡的,但我在检查第一次出现的行号时遇到了问题。我知道我必须写下,如果下一个符号是“或/”,我会忽略这一行,或者直到我点击下一行,但我不确定正确的语法。如有任何帮助,将不胜感激。以下是我目前掌握的信息: import java.awt.*; import java.awt.event.*; import javax.swing.*; i
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;
}
}
}
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
导入java.io.*;
导入javax.swing.filechooser.*;
导入javax.swing.text.BadLocationException;
公共类排序扩展了JFrame实现ActionListener{
私有堆栈符号;
JTextArea opentextArea;
JTextArea placetextArea;
项目开放;
final JFileChooser fc=新的JFileChooser();
公共静态void main(字符串[]args)
{
排序gui=新排序();
}
公共分拣()
{
JFrame=新JFrame();
setLayout(新网格布局(1,2));
setTitle(“堆栈排序”);
JMenuBar menuBar=新的JMenuBar();
JMenu menuItems=新JMenu(“文件”);
打开=新项目(“打开”);
open.addActionListener(this);
JMenuItem重置=新的JMenuItem(“重置”);
reset.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件e)
{
opentextArea.setText(“”);
placetextArea.setText(“”);
}
}
);
menuItems.add(打开);
菜单项添加(重置);
菜单栏。添加(菜单项);
setJMenuBar(菜单栏);
opentextArea=新的JTextArea();
placetextArea=新的JTextArea();
添加(opentextArea);
添加(placetextArea);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
包装();
setVisible(真);
}
已执行的公共无效操作(操作事件)
{
int returnVal=fc.showOpenDialog(此);
if(returnVal==JFileChooser.APPROVE_选项)
{
File File=fc.getSelectedFile();
FileInputStream为空;
尝试
{
is=新文件输入流(文件);
}
catch(filenotfounde异常)
{
System.out.println(“异常:+e.toString());
}
字节[]nextChar=新字节[2];
尝试
{
int值=is.read(nextChar);
int num=1;
追加(num+“:”);
while(值!=-1)
{
String newString=新字符串(nextChar);
如果(nextChar[0]='\n'| | nextChar[0]='\r')
{
num++;
append(newString+num+“:”);
}
其他的
opentextArea.append(新闻字符串);
值=is.read(nextChar);
}
}
捕获(IOE异常)
{
System.out.println(“异常:+e.toString());
}
堆栈=新堆栈();
字符串s=opentextArea.getText();
int指数=0;
int numline=1;
而(索引
这是我编辑的代码:
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;
}
}
}
}
Stack Stack=新堆栈();
字符串s=opentextArea.getText();
int指数=0;
int numline=1;
而(索引