Need some help finshing up a java program I have most of it done however having
ID: 3837099 • Letter: N
Question
Need some help finshing up a java program I have most of it done however having issues with the graphics.
Here are the requirments.
In this program, you are going to read in a file, and you are going to parse it onto a stack and determine if it is syntactically correct. You will check for all open and close curly braces, open and closed parentheses, and you will check for all double quotes. You should also allow the user to choose their own file. This should be done graphically. Please use your own implementation of a stack.
Start with something like this:
public class MainFrame {
public static void main(String[] args) {
// Create an instance of the frame
JFrame myFrame = new JFrame("Basic Example");
// set the default close operation (i.e. when they click the x button) myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the panel to the frame myFrame.getContentPane().add(new MainPanel());
// this make sure whatever is in the panel fits in the frame myFrame.pack();
// make sure the frame is visible - it's not visible by default myFrame.setVisible(true);
}
My Code:
Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
initComponents();
}
private void initComponents() {
fileChooser = new javax.swing.JFileChooser();
browsButton = new javax.swing.JButton();
browseTextField = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
fileTextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
browsButton.setText("Browse");
browsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browsButtonActionPerformed(evt);
}
});
browseTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseTextFieldActionPerformed(evt);
}
});
fileTextArea.setColumns(20);
fileTextArea.setRows(5);
jScrollPane1.setViewportView(fileTextArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(browseTextField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(browsButton))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)))
.addGap(26, 26, 26))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(browsButton)
.addComponent(browseTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
private void browseTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void setFile(File file) {
browseTextField.setText(file.getPath());
}
private void putIntoStack(File file) {
String string = "";
ArrayList < String > fileList = new ArrayList < String > ();
try {
Scanner scanner = new Scanner(new File("brackets.txt"));
while (scanner.hasNext()) {
string = scanner.next();
fileList.add(string);
}
scanner.close();
fileTextArea.setText(fileList.toString());
String strArrString[] = fileList.toArray(new String[0]);
for (String str: strArrString) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
} catch (NullPointerException e) {
}
}
private void browsButtonActionPerformed(java.awt.event.ActionEvent evt) {
fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file != null) {
setFile(file);
}
putIntoStack(file);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main Main = new Main();
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
private javax.swing.JButton browsButton;
private static javax.swing.JTextField browseTextField;
private javax.swing.JFileChooser fileChooser;
private javax.swing.JTextArea fileTextArea;
private javax.swing.JScrollPane jScrollPane1;
}
brackets.txt
(This file has brackets and {some que})
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class Main extends JFrame {
public Main() {
initComponents();
}
private void initComponents() {
fileChooser = new javax.swing.JFileChooser();
browsButton = new javax.swing.JButton();
browseTextField = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
fileTextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
browsButton.setText("Browse");
browsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browsButtonActionPerformed(evt);
}
});
browseTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseTextFieldActionPerformed(evt);
}
});
fileTextArea.setColumns(20);
fileTextArea.setRows(5);
jScrollPane1.setViewportView(fileTextArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(browseTextField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(browsButton))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)))
.addGap(26, 26, 26))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(browsButton)
.addComponent(browseTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
private void browseTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void setFile(File file) {
browseTextField.setText(file.getPath());
}
}
}
private void browsButtonActionPerformed(java.awt.event.ActionEvent evt) {
fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file != null) {
setFile(file);
}
putIntoStack(file);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main Main = new Main();
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.