Calculator will not answer new problems - java

The calculator I am working on will solve the first problem perfectly, but when add is hit a second time, the answer moves closer to the addends and both the addends and the sum remain the same. Can anyone shine some light on what is making my program act this way? Thanks!
public class GUI extends JFrame implements ActionListener{
private JTextField field1;
private JTextField field2;
private JButton add, subtract, multiply, divide;
private JLabel lanswer, label1, label2;
private String input1, input2, sanswer;
private int answer = 0;
JPanel contentPanel, answerPanel;
public GUI(){
super("Calculator");
field1 = new JTextField(null, 15);
field2 = new JTextField(null, 15);
add = new JButton("add");
subtract = new JButton("subtract");
multiply = new JButton("multiply");
divide = new JButton("divide");
lanswer = new JLabel("", SwingConstants.CENTER);
label1 = new JLabel("Value 1:");
label2 = new JLabel("Value 2:");
add.addActionListener(this);
Dimension opSize = new Dimension(110, 20);
Dimension inSize = new Dimension(200, 20);
lanswer.setPreferredSize(new Dimension(200,255));
field1.setPreferredSize(inSize);
field2.setPreferredSize(inSize);
add.setPreferredSize(opSize);
subtract.setPreferredSize(opSize);
multiply.setPreferredSize(opSize);
divide.setPreferredSize(opSize);
contentPanel = new JPanel();
contentPanel.setBackground(Color.PINK);
contentPanel.setLayout(new FlowLayout());
answerPanel = new JPanel();
answerPanel.setPreferredSize(new Dimension(230, 260));
answerPanel.setBackground(Color.WHITE);
answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.Y_AXIS));
contentPanel.add(answerPanel);
contentPanel.add(label1); contentPanel.add(field1);
contentPanel.add(label2); contentPanel.add(field2);
contentPanel.add(add); contentPanel.add(subtract); contentPanel.add(multiply); contentPanel.add(divide);
this.setContentPane(contentPanel);
}
public void actionPerformed(ActionEvent e) {
JButton src = (JButton)e.getSource();
if(src.equals(add)){
add();
}
}
private void add(){
input1 = field1.getText();
input2 = field2.getText();
MathEquation problem = new MathEquation(input1, input2);
Dimension lineSize = new Dimension(200, 10);
JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE);
line1.setPreferredSize(lineSize);
JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE);
line2.setPreferredSize(lineSize);
JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE);
line3.setPreferredSize(lineSize);
JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE);
line4.setPreferredSize(lineSize);
JPanel line5 = new JPanel(); line5.setBackground(Color.WHITE);
JLabel[] sumLabels = problem.getSumLabels();
JLabel[] addend1Labels = problem.getAddend1Labels();
JLabel[] addend2Labels = problem.getAddend2Labels();
JLabel[] carriedLabels = problem.getCarriedLabels();
for(int i = 0; i < carriedLabels.length; i++){
line1.add(carriedLabels[i]);
}
for(int i = 0; i < addend1Labels.length; i++){
line2.add(addend1Labels[i]);
}
for(int i = 0; i < addend2Labels.length; i++){
line3.add(addend2Labels[i]);
}
String answerLine = "__";
for(int i = 0; i < sumLabels.length; i++){
answerLine += "__";
}
line4.add(new JLabel(answerLine));
for(int i = 0; i < sumLabels.length; i++){
line5.add(sumLabels[i]);
}
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(line1);
answerPanel.add(line1);
answerPanel.add(line2);
answerPanel.add(line3);
answerPanel.add(line4);
answerPanel.add(line5);
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
this.setContentPane(contentPanel);
this.revalidate();
}
private String subtract(){
input1 = field1.getText();
input2 = field2.getText();
answer = Integer.parseInt(input1) - Integer.parseInt(input2);
sanswer = "<html><p align='right'>" + input1 + "<br>-+";
if(input1.length() >= input2.length()){
for(int i = 0; i < input1.length() - input2.length() + 1; i++){
sanswer+=" ";
}
}
sanswer += input2 + "<br>";
if(input1.length() >= input2.length()){
for(int i = 0; i < input1.length(); i++){
sanswer+="--";
}
}
else
{
for(int i = 0; i < input2.length(); i++){
sanswer+="--";
}
}
sanswer += "<br>" + answer + "</p></html>";
return sanswer;
}
private String multiply(){
input1 = field1.getText();
input2 = field2.getText();
answer = Integer.parseInt(input1) * Integer.parseInt(input2);
sanswer = "<html><p align='right'>" + input1 + "<br>x";
if(input1.length() >= input2.length()){
for(int i = 0; i < input1.length() - input2.length() + 1; i++){
sanswer+=" ";
}
}
sanswer += input2 + "<br>";
if(input1.length() >= input2.length()){
for(int i = 0; i < input1.length(); i++){
sanswer+="--";
}
}
else
{
for(int i = 0; i < input2.length(); i++){
sanswer+="--";
}
}
sanswer += "<br>" + answer + "</p></html>";
return sanswer;
}
}
MathEquation:
public class MathEquation {
private ArrayList<Character> addend1, addend2, sum, carried;
public MathEquation(String a, String b){
setAddends(a, b);
findSum();
System.out.println(carried.toString());
System.out.println(" "+addend1.toString());
System.out.println(" "+addend2.toString());
System.out.println(" "+sum.toString());
}
public JLabel[] getAddend1Labels(){
Dimension addendSize = new Dimension(10,10);
JLabel[] getAddend1 = new JLabel[addend1.size()];
for(int i = 0; i < addend1.size(); i++){
getAddend1[i] = new JLabel(addend1.get(i).toString());
getAddend1[i].setPreferredSize(addendSize);
}
return getAddend1;
}
public JLabel[] getAddend2Labels(){
Dimension addendSize = new Dimension(10,10);
JLabel[] getAddend2 = new JLabel[addend2.size()];
for(int i = 0; i < addend2.size(); i++){
getAddend2[i] = new JLabel(addend2.get(i).toString());
getAddend2[i].setPreferredSize(addendSize);
}
return getAddend2;
}
public JLabel[] getSumLabels(){
Dimension sumSize = new Dimension(10,10);
JLabel[] getSum = new JLabel[sum.size()];
for(int i = 0; i < sum.size(); i++){
getSum[i] = new JLabel(sum.get(i).toString());
getSum[i].setPreferredSize(sumSize);
}
return getSum;
}
public JLabel[] getCarriedLabels(){
Dimension carriedSize = new Dimension(10,10);
JLabel[] getCarried = new JLabel[carried.size()];
for(int i = 0; i < carried.size(); i++){
getCarried[i] = new JLabel(carried.get(i).toString());
getCarried[i].setPreferredSize(carriedSize);
}
return getCarried;
}
public void setAddends(String a, String b){
char[] arrayA = a.toCharArray();
char[] arrayB = b.toCharArray();
addend1 = new ArrayList<Character>();
addend2 = new ArrayList<Character>();
ArrayList wholeA = new ArrayList();
ArrayList wholeB = new ArrayList();
boolean wholeNumber = true;
ArrayList decimalA = new ArrayList();
ArrayList decimalB = new ArrayList();
int decALength = 0, decBLength = 0;
for(int i = 0; i < arrayA.length; i++){
if(arrayA[i] != '.'){
addend1.add(arrayA[i]);
if(wholeNumber){
wholeA.add(arrayA[i]);
} else {
decimalA.add(arrayA[i]);
}
} else {
addend1.add(arrayA[i]);
wholeNumber = false;
decALength = Arrays.copyOfRange(arrayA, i+1, arrayA.length).length;
System.out.println(decALength);
}
}
wholeNumber = true;
for(int i = 0; i < arrayB.length; i++){
if(arrayB[i] != '.'){
addend2.add(arrayB[i]);
if(wholeNumber){
wholeB.add(arrayB[i]);
} else {
decimalB.add(arrayB[i]);
}
} else {
addend2.add(arrayB[i]);
wholeNumber = false;
decBLength = Arrays.copyOfRange(arrayB, i+1, arrayB.length).length;
System.out.println(decBLength);
}
}
if(decALength>decBLength){
if(decBLength==0){
addend2.add('.');
}
for(int i = 0; i < decALength-decBLength; i++){
addend2.add('0');
}
} else if(decBLength>decALength){
if(decALength==0){
addend1.add('.');
}
for(int i = 0; i < decBLength-decALength; i++){
addend1.add('0');
}
}
if(wholeA.size() < wholeB.size())
for(int i = 0; i < wholeB.size()-wholeA.size(); i++)
addend1.add(0, '0');
else if(wholeA.size() > wholeB.size())
for(int i = 0; i < wholeA.size()-wholeB.size(); i++)
addend2.add(0, '0');
addend1.add(0, '0');
addend2.add(0, '0');
}
private void findSum(){
sum = new ArrayList<Character>();
carried = new ArrayList<Character>();
int dec = 0;
carried.add('0');
for(int i = addend1.size()-1; i >= 0; i--){
if(addend1.get(i) != '.'){
int a = Character.getNumericValue(addend1.get(i));
int b = Character.getNumericValue(addend2.get(i));
int c = Character.getNumericValue(carried.get(carried.size()-1));
int d = a + b + c;
if(d >= 10){
carried.add('1');
d-=10;
sum.add((char) ('0' + d));
} else {
carried.add('0');
sum.add((char) ('0' + d));
}
} else {
sum.add('.');
carried.add(carried.size()-1, ' ');
}
}
Collections.reverse(sum);
Collections.reverse(carried);
carried.remove(0);
for(int i = 0; i < carried.size(); i++){
if(carried.get(i) == '0'){
carried.set(i, ' ');
}
}
for(int i = 0; i < addend1.size(); i++){
if(addend1.get(i) == '0'){
addend1.set(i, ' ');
}
else break;
}
for(int i = 0; i < addend2.size(); i++){
if(addend2.get(i) == '0'){
addend2.set(i, ' ');
}
else break;
}
for(int i = 0; i < sum.size(); i++){
if(sum.get(i) == '0'){
sum.set(i, ' ');
}
else break;
}
}
}

Related

JScrollPane array in JOptionPane.showMessageDialog

In my code, I'm trying to use a JScrollPane and add it into a JOptionPane.showMessageDialog. I don't know if this is possible to start with but I don't know how to do it if it is, and how to add the whole array into the one message and not multiple. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
public class CulminatingPro implements ActionListener
{
//Create an array of buttons
static JButton[][] buttons = new JButton[10][4];
static JButton jbtList = new JButton("List");
ArrayList<Culminating> flyers = new ArrayList<Culminating>();
String fn;
String ln;
String pn;
String adress;
int column;
int row;
int reply;
int v;
Object[] options = {"First Name",
"Last Name",
"Phone Number",
"Adress"};
public static void main(String[] args)
{
JFrame frame = new JFrame("Fly By Buddies");
frame.setSize(900, 900);
JPanel mainPanel = new JPanel();
mainPanel.setLayout( new GridLayout(1,2));
JPanel paneSeats = new JPanel();
JPanel paneInfo = new JPanel();
paneSeats.setLayout( new GridLayout(11, 4, 5,5));
mainPanel.add(paneSeats);
mainPanel.add(paneInfo);
frame.setContentPane(mainPanel);
for (int i = 0; i < buttons.length; i++)
{
for(int j = 0; j < buttons[0].length; j++)
{
if (j + 1 == 1)
{
buttons[i][j] = new JButton("Seat " + (i + 1) + "A");
buttons[i][j].setBackground(Color.GREEN);
paneSeats.add(buttons[i][j]);
buttons[i][j].addActionListener(new CulminatingPro());
}
else if (j + 1 == 2)
{
buttons[i][j] = new JButton("Seat " + (i + 1) + "B");
buttons[i][j].setBackground(Color.GREEN);
paneSeats.add(buttons[i][j]);
buttons[i][j].addActionListener(new CulminatingPro());
}
else if (j + 1== 3)
{
buttons[i][j] = new JButton("Seat " + (i + 1) + "C");
buttons[i][j].setBackground(Color.GREEN);
paneSeats.add(buttons[i][j]);
buttons[i][j].addActionListener(new CulminatingPro());
}
else if (j + 1== 4)
{
buttons[i][j] = new JButton("Seat " + (i + 1) + "D");
buttons[i][j].setBackground(Color.GREEN);
paneSeats.add(buttons[i][j]);
buttons[i][j].addActionListener(new CulminatingPro());
}
}
}
jbtList.setPreferredSize(new Dimension(400, 40));
jbtList.addActionListener(new CulminatingPro());
paneInfo.add(jbtList, BorderLayout.SOUTH);
frame.setVisible(true);
frame.toFront();
}
public void actionPerformed(ActionEvent event)
{
for (int i = 0; i < buttons.length; i++)
{
for(int j = 0; j < buttons[0].length; j++)
{
if (event.getSource() == buttons[i][j])
{
v = 0;
for (int k = 0; k < flyers.size();k++)
{
if((flyers.get(k).getColumn() == (j) && (flyers.get(k).getColumn() == (j))))
{
if (!flyers.get(k).getFirstName().equals(""))
{
reply = JOptionPane.showConfirmDialog(null,
"Would you like to delete the info on the passenger?", "Deletion", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
flyers.remove(k);
buttons[i][j].setBackground(Color.GREEN);
}
else if (reply == JOptionPane.NO_OPTION)
{
reply = JOptionPane.showConfirmDialog(null,
"Would you like to modify the info on the passenger?", "Modification", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
int n = JOptionPane.showOptionDialog(null, "Message", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
n = n + 1;
if(n == 0)
{
fn = JOptionPane.showInputDialog(null,"Re-enter passenger's first name: ");
flyers.get(k).setFirstName(fn);
}
if(n == 2)
{
ln = JOptionPane.showInputDialog(null,"Re-enter passenger's last name: ");
flyers.get(k).setLastName(ln);
}
if(n == 3)
{
pn = JOptionPane.showInputDialog(null,"Re-enter passenger's phone number: ");
flyers.get(k).setPhoneNumber(pn);
}
if(n == 4)
{
adress = JOptionPane.showInputDialog(null,"Re-enter passenger's adress: ");
flyers.get(k).setAdress(adress);
}
}
}
v = 1;
}
}
}
if(v == 0)
{
fn = JOptionPane.showInputDialog(null,"Passenger's first name (Necessary): ");
ln = JOptionPane.showInputDialog(null,"Passenger's last name (Necessary): ");
pn = JOptionPane.showInputDialog(null,"Passenger's phone number: ");
adress = JOptionPane.showInputDialog(null,"Passenger's adress: ");
column = j;
row = i;
flyers.add(new Culminating(fn,ln,pn,adress,column,row));
buttons[i][j].setBackground(Color.RED);
}
}
}
}
if (event.getSource().equals("List"))
{
JScrollPane[] scroll = new JScrollPane[flyers.size()];
for(int p = 0; p < flyers.size(); p++)
{
JTextArea text = new JTextArea(flyers.get(p).toString(), 10, 40);
scroll[p] = new JScrollPane(text);
}
for(int p = 0; p < flyers.size(); p++)
{
JOptionPane.showMessageDialog(null,scroll[p]);
}
}
}
}

Can I set array in class from inside action listener? JAVA

Basically all I need to do is get array1 and array2 from each of my buttonlisteners and I have no clue how to do this as it is.. Right now the TextMatch button doesn't work because array1 and array2 are empty, is there a way to set them from the buttonlistener classes?
All I need is a way to set array1 and array2 as ss and sstwo so I can implement my TextTools.match() method
MAIN PROGRAM:
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JComboBox;
public class Lab11 extends JPanel
{
private SuperString [] array1, array2;
private StringBuilder string, string2, string3, string4; //loads SuperStrings faster by appending all at once
private JRadioButton occurrence, alphabetical;
private JPanel text;
private JComboBox<Integer> input;
private JLabel label, file1,file2, unique, unique2,textmatch;
private JButton load, go,go2, TextMatch;
private CountLinkedList<SuperString> words, words3; //Change impliments CountList to extends BinaryCountTree
private OrderedLinkedList<SuperString> words2, words4;//Change impliments CountList to extends BinaryCountTree
private String filename,filename2;
private int width = 450;
private int height = 550;
private TextArea textarea,textarea2;
Scanner scan;
public Lab11()
{
TextListener textlistener = new TextListener();
ButtonListener listener = new ButtonListener();
Button2Listener listener2 = new Button2Listener();
Integer [] select = {1,2,3,4};
input = new JComboBox<Integer>(select);
text = new JPanel(new GridLayout(1,2));
go = new JButton("Select Text File 1: ");
go2 = new JButton("Select Text File 2: ");
TextMatch = new JButton("TextMatch");
textmatch = new JLabel("");
label = new JLabel("N: " );
unique = new JLabel("");
unique2 = new JLabel("");
file1 = new JLabel("");
file1.setFont(new Font("Helvetica",Font.PLAIN,24));
unique.setFont(new Font("Helvetica",Font.PLAIN,24));
file2 = new JLabel("");
file2.setFont(new Font("Helvetica",Font.PLAIN,24));
unique2.setFont(new Font("Helvetica",Font.PLAIN,24));
occurrence= new JRadioButton("Occurrence");
occurrence.setMnemonic(KeyEvent.VK_O);
occurrence.addActionListener(listener);
occurrence.addActionListener(listener2);
occurrence.setSelected(true);
alphabetical = new JRadioButton("Alphabetical");
alphabetical.setMnemonic(KeyEvent.VK_A);
alphabetical.addActionListener(listener);
alphabetical.addActionListener(listener2);
ButtonGroup group = new ButtonGroup();
group.add(occurrence);
group.add(alphabetical);
go.addActionListener(listener);
go2.addActionListener(listener2);
input.addActionListener(listener);
input.addActionListener(listener2);
TextMatch.addActionListener(textlistener);
textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
textarea2.setFont(new Font("Helvetica",Font.PLAIN,24));
textarea.setPreferredSize(new Dimension(width,height));
textarea2.setPreferredSize(new Dimension(width,height));
setPreferredSize(new Dimension(1000,700));
text.add(textarea);
text.add(textarea2);
add(occurrence);
add(alphabetical);
add(label);
add(input);
add(go);
add(file1);
add(unique);
add(go2);
add(file2);
add(unique2);
add(TextMatch);
add(textmatch);
add(text);
textarea.setText("No File Selected");
textarea2.setText("No File Selected");
}
public class ButtonListener implements ActionListener //makes buttons do things
{
JFileChooser chooser = new JFileChooser("../Text");
public void actionPerformed(ActionEvent event)
{
Integer N = input.getSelectedIndex()+1;
string = new StringBuilder();
string2 = new StringBuilder();
if(event.getSource() == go)
{
int returnvalue = chooser.showOpenDialog(null);
if(returnvalue == JFileChooser.APPROVE_OPTION)
{
try
{
File file = chooser.getSelectedFile();
String text1= file.getName();
file1.setText(text1);
filename = file.getName();
System.err.println(filename);
scan = new Scanner(file);
}
catch (IOException e)
{
System.err.println("IO EXCEPTION");
return;
}
}
else
{
return;
}
String[] storage = new String[N];
words = new CountLinkedList<SuperString>();
words2 = new OrderedLinkedList<SuperString>();
for(int i=1;i<N;i++)
storage[i] = scan.next().toLowerCase().replaceAll("[^A-Za-z0-9]", "")
.replaceAll("[.,':]","");
while(scan.hasNext())
{
for(int i=0;i<=N-2;i++)
storage[i] = storage[i+1];
storage[N-1] = scan.next().toLowerCase();
storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
.replaceAll("[.,':]","");
SuperString ss = new SuperString(storage);
SuperString ss2= new SuperString(storage);
words.add(ss );
words2.add(ss2 );
}
}
SuperString[] array1 = new SuperString[words.size()];
SuperString[] ss = new SuperString[words.size()];
SuperString[] ss2 = new SuperString[words2.size()];
int i=0;
int count =0, count2= 0;
for(SuperString word: words)
{
ss[i] = word;
array1[i] = word;
i++;
}
int j=0;
for(SuperString word: words2)
{
ss2[j] = word;
j++;
}
Arrays.sort(ss, new SuperStringCountOrder());
for(SuperString word : ss)
{
count++;
string.append(Integer.toString(count)+ " "+ word+ "\n");
}
if(occurrence.isSelected())
{
textarea.setText("");
textarea.append(" "+filename+" has wordcount: "+words.size()+
"\n-------------------------\n\n");
textarea.append(string.toString());
string.replace(0,string.length()*2, "");
}
for(SuperString word : ss2)
{
count2++;
string2.append(Integer.toString(count2)+ " "+ word.toString()+ "\n");
}
if(alphabetical.isSelected())
{
textarea.setText("");
textarea.append(" "+filename+" has wordcount: "+words.size()+
"\n-------------------------\n\n");
textarea.append(string2.toString());
string2.replace(0,string2.length()*2, "");
}
unique.setText("Unique Count: "+ Integer.toString(words.size()));
}
}
public class Button2Listener implements ActionListener
{
JFileChooser chooser = new JFileChooser("../Text");
public void actionPerformed(ActionEvent event)
{
Integer N = input.getSelectedIndex()+1;
string3 = new StringBuilder();
string4 = new StringBuilder();
if(event.getSource() == go2)
{
int returnvalue = chooser.showOpenDialog(null);
if(returnvalue == JFileChooser.APPROVE_OPTION)
{
try
{
File file = chooser.getSelectedFile();
String text2= file.getName();
file2.setText(text2);
filename2 = file.getName();
System.err.println(filename);
scan = new Scanner(file);
}
catch (IOException e)
{
System.err.println("IO EXCEPTION");
return;
}
}
else
{
return;
}
String[] storage = new String[N];
words3 = new CountLinkedList<SuperString>();
words4 = new OrderedLinkedList<SuperString>();
for(int i=1;i<N;i++)
storage[i] = scan.next().toLowerCase().replace(",","").replace(".","");
while(scan.hasNext())
{
for(int i=0;i<=N-2;i++)
storage[i] = storage[i+1];
storage[N-1] = scan.next().toLowerCase();
storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
.replaceAll("[.,':]","");
SuperString ss = new SuperString(storage);
SuperString ss2= new SuperString(storage);
words3.add(ss );
words4.add(ss2 );
}
textarea2.setText("");
}
SuperString[] array2 = new SuperString[words3.size()];
SuperString[] sstwo = new SuperString[words3.size()];
SuperString[] ss2two = new SuperString[words4.size()];
int i=0;
int count =0, count2= 0;
for(SuperString word2: words3)
{
sstwo[i] = word2;
array2[i] = word2;
i++;
}
int j=0;
for(SuperString word2: words4)
{
ss2two[j] = word2;
j++;
}
Arrays.sort(sstwo, new SuperStringCountOrder());
for(SuperString word2 : sstwo)
{
count++;
string3.append(Integer.toString(count)+ " "+ word2+ "\n");
}
if(occurrence.isSelected())
{
textarea2.setText("");
textarea2.append(" "+filename2+" has wordcount: "+words3.size()+
"\n-------------------------\n\n");
textarea2.append(string3.toString());
}
for(SuperString word2 : ss2two)
{
count2++;
string4.append(count2+" "+ " "+word2+"\n");
}
if(alphabetical.isSelected())
{
textarea2.setText("");
textarea2.append(" "+filename2+" has wordcount: "+words3.size()+
"\n-------------------------\n\n");
textarea2.append(string4.toString());
}
unique2.setText("Unique Count: "+ Integer.toString(words3.size()));
}
}
public class TextListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() ==TextMatch)
{
textmatch.setText("The match is: "+ TextTools.match(array1,array2));
}
}
}
public static void main(String[] arg)
{
JFrame frame = new JFrame("Lab 11");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Lab11());
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
}
TEXT MATCH PROGRAM:
import java.util.Arrays;
public class TextTools
{
public static double match(SuperString[] X, SuperString[] Y)
{
double length1 = 0, length2 = 0;
Arrays.sort(X);
Arrays.sort(Y);
for(SuperString token : X)
{
double value = token.count();
length1 += value*value;
}
length1 = Math.sqrt(length1);
for(SuperString token : Y)
{
double value = token.count();
length2 += value*value;
}
length2 = Math.sqrt(length2);
double total = 0;
int j=0;
for(int i=0;i<X.length;i++)
{
while(j < Y.length && X[i].compareTo(Y[j])>0 )
j++;
if(j == Y.length)
return total/(length1*length2);
if(X[i].compareTo(Y[j]) == 0)
total += X[i].count()*Y[j].count();
}
return total/(length1*length2);
}
}
Turns out I'm erasing over my array1 and array2 by re declaring SuperString in the actionlistener

Checkbox Compilation Error

I am to stimulate a java applet of a menu with checkboxes and checkboxgroups, with calories listed next to each food choice. My problem is I cannot figure out why I keep getting this error message when i compile it:
AnAppletWithCheckboxes.java:188: illegal forward reference
int crepeVal = Integer.parseInt(textField.getText());
I get that for every line that I have is converting the textField into an int, so hence there are 17 errors. Any idea of why will be appreciated!
Here is my Code:: it is very long because there are plenty checkboxes and textFields. I commented Out My place of Error! :
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithCheckboxes extends Applet implements ItemListener {
public void init() {
setLayout(new GridLayout(1, 0));
CheckboxGroup dinnerType = new CheckboxGroup();
standard = new Checkbox("standard", dinnerType, false);
standard.addItemListener(this);
deluxe = new Checkbox("deluxe", dinnerType, true);
deluxe.addItemListener(this);
CheckboxGroup appetizers = new CheckboxGroup();
crepe = new Checkbox("crepe", appetizers, false);
crepe.addItemListener(this);
quiche = new Checkbox("quiche", appetizers, false);
quiche.addItemListener(this);
dumpling = new Checkbox("dumpling", appetizers, false);
dumpling.addItemListener(this);
textField = new TextField("300");
textField.setEditable(false);
textField2 = new TextField("330");
textField2.setEditable(false);
textField3 = new TextField("98");
textField3.setEditable(false);
CheckboxGroup soupOrSalad = new CheckboxGroup();
soup = new Checkbox("Soup", soupOrSalad, false);
soup.addItemListener(this);
CheckboxGroup soups = new CheckboxGroup();
cream = new Checkbox("cream", soups, false);
cream.addItemListener(this);
broth = new Checkbox("broth", soups, false);
broth.addItemListener(this);
gumbo = new Checkbox("gumbo", soups, false);
gumbo.addItemListener(this);
textField4 = new TextField("190");
textField4.setEditable(false);
textField5 = new TextField("20");
textField5.setEditable(false);
textField6 = new TextField("110");
textField6.setEditable(false);
salad = new Checkbox("Salad", soupOrSalad, false);
salad.addItemListener(this);
CheckboxGroup salads = new CheckboxGroup();
tossed = new Checkbox("tossed", salads, false);
tossed.addItemListener(this);
caesar = new Checkbox("caesar", salads, false);
caesar.addItemListener(this);
croutons = new Checkbox("croutons");
croutons.addItemListener(this);
lite = new Checkbox("lite dressing");
lite.addItemListener(this);
textField7 = new TextField("35");
textField7.setEditable(false);
textField8 = new TextField("90");
textField8.setEditable(false);
textField9 = new TextField("60");
textField9.setEditable(false);
textField10 = new TextField("50");
textField10.setEditable(false);
CheckboxGroup entrees = new CheckboxGroup();
chicken = new Checkbox("chicken", entrees, false);
chicken.addItemListener(this);
beef = new Checkbox("beef", entrees, false);
beef.addItemListener(this);
lamb = new Checkbox("lamb", entrees, false);
lamb.addItemListener(this);
fish = new Checkbox("fish", entrees, false);
fish.addItemListener(this);
textField11 = new TextField("200");
textField11.setEditable(false);
textField12 = new TextField("170");
textField12.setEditable(false);
textField13 = new TextField("65");
textField13.setEditable(false);
textField14 = new TextField("150");
textField14.setEditable(false);
CheckboxGroup deserts = new CheckboxGroup();
pie = new Checkbox("pie", deserts, false);
pie.addItemListener(this);
fruit = new Checkbox("fruit", deserts, false);
fruit.addItemListener(this);
sherbet = new Checkbox("sherbet", deserts, false);
sherbet.addItemListener(this);
textField15 = new TextField("80");
textField15.setEditable(false);
textField16 = new TextField("60");
textField16.setEditable(false);
textField17 = new TextField("107");
textField17.setEditable(false);
calories = new Button("Calories");
calTextField = new TextField("0"+total);
calTextField.setEditable(false);
setLayout(new GridLayout(0, 1));
Panel p = new Panel();
add(p);
p.add(standard);
p.add(deluxe);
appetizerPanel = new Panel();
add(appetizerPanel);
label = new Label("Appetizer");
appetizerPanel.add(label);
appetizerPanel.add(crepe);
appetizerPanel.add(textField);
appetizerPanel.add(quiche);
appetizerPanel.add(textField2);
appetizerPanel.add(dumpling);
appetizerPanel.add(textField3);
soupPanel = new Panel();
add(soupPanel);
soupPanel.add(soup);
soupPanel.add(cream);
soupPanel.add(textField4);
soupPanel.add(broth);
soupPanel.add(textField5);
soupPanel.add(gumbo);
soupPanel.add(textField6);
saladPanel = new Panel();
add(saladPanel);
saladPanel.add(salad);
saladPanel.add(tossed);
saladPanel.add(textField7);
saladPanel.add(caesar);
saladPanel.add(textField8);
saladPanel.add(croutons);
saladPanel.add(textField9);
saladPanel.add(lite);
saladPanel.add(textField10);
entreePanel = new Panel();
add(entreePanel);
label2 = new Label("Entree");
entreePanel.add(label2);
entreePanel.add(chicken);
entreePanel.add(textField11);
entreePanel.add(beef);
entreePanel.add(textField12);
entreePanel.add(lamb);
entreePanel.add(textField13);
entreePanel.add(fish);
entreePanel.add(textField14);
desertPanel = new Panel();
add(desertPanel);
label3 = new Label("Desert");
desertPanel.add(label3);
desertPanel.add(pie);
desertPanel.add(textField15);
desertPanel.add(fruit);
desertPanel.add(textField16);
desertPanel.add(sherbet);
desertPanel.add(textField17);
caloriesPanel = new Panel();
add(caloriesPanel);
caloriesPanel.add(calories);
caloriesPanel.add(calTextField);
}
public void LabelChange(Label b) {
if (b ==label3)
b.setForeground(Color.lightGray);
else
label3.setForeground(Color.black);
}
/* This is where i get those errors!! */
int crepeVal = Integer.parseInt(textField.getText());
int quicheVal = Integer.parseInt(textField2.getText());
int dumplingVal = Integer.parseInt(textField3.getText());
int creamVal = Integer.parseInt(textField4.getText());
int brothVal = Integer.parseInt(textField5.getText());
int gumboVal = Integer.parseInt(textField6.getText());
int tossedVal = Integer.parseInt(textField7.getText());
int caesarVal = Integer.parseInt(textField8.getText());
int croutonsVal = Integer.parseInt(textField9.getText());
int liteVal = Integer.parseInt(textField10.getText());
int chickenVal = Integer.parseInt(textField11.getText());
int beefVal = Integer.parseInt(textField12.getText());
int lambVal = Integer.parseInt(textField13.getText());
int fishVal = Integer.parseInt(textField14.getText());
int pieVal = Integer.parseInt(textField15.getText());
int fruitVal = Integer.parseInt(textField16.getText());
int sherbetVal = Integer.parseInt(textField17.getText());
public boolean action(Event evt, Object whatAction){
if(!(evt.target instanceof Button)){
return false;
}
else {
calorieCount();
return true;
}
}
public void calorieCount () {
if (crepe.getState())
crepeVal += total;
else
crepeVal = 0;
if (quiche.getState())
quicheVal += total;
else
quicheVal = 0;
if (dumpling.getState())
dumplingVal += total;
else
dumplingVal= 0;
if (cream.getState())
creamVal += total;
else
creamVal = 0;
if (broth.getState())
brothVal += total;
else
brothVal = 0;
if (gumbo.getState())
gumboVal += total;
else
gumboVal = 0;
if (tossed.getState())
tossedVal += total;
else
tossedVal = 0;
if (caesar.getState())
caesarVal += total;
else
caesarVal = 0;
if (croutons.getState())
croutonsVal += total;
else
croutonsVal = 0;
if (lite.getState())
liteVal += total;
else
liteVal = 0;
if (chicken.getState())
chickenVal += total;
else
chickenVal = 0;
if (beef.getState())
beefVal += total;
else
beefVal = 0;
if (lamb.getState())
lambVal += total;
else
lambVal = 0;
if (fish.getState())
fishVal += total;
else
fishVal = 0;
if (pie.getState())
pieVal += total;
else
pieVal = 0;
if (fruit.getState())
fruitVal += total;
else
fruitVal = 0;
if (sherbet.getState())
sherbetVal += total;
else
sherbetVal = 0;
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == standard || e.getSource() == deluxe)
handleDinnerType((Checkbox)e.getSource());
else if (e.getSource() == soup || e.getSource() == salad)
handleSoupSaladChoice((Checkbox)e.getSource());
}
void handleDinnerType(Checkbox selectedType) {
boolean enabled;
if (selectedType == standard){
enabled = false;
LabelChange(label3);
}
else{
enabled = true;
LabelChange(label);
}
soup.setEnabled(enabled);
salad.setEnabled(enabled);
pie.setEnabled(enabled);
fruit.setEnabled(enabled);
sherbet.setEnabled(enabled);
cream.setEnabled(enabled);
broth.setEnabled(enabled);
gumbo.setEnabled(enabled);
tossed.setEnabled(enabled);
caesar.setEnabled(enabled);
croutons.setEnabled(enabled);
lite.setEnabled(enabled);
}
void handleSoupSaladChoice(Checkbox selectedCourse) {
boolean soupEnabled, saladEnabled;
if (selectedCourse == soup) {
soupEnabled = true;
saladEnabled = false;
soup.setForeground(Color.BLACK);
salad.setForeground(Color.lightGray);
}
else {
soupEnabled = false;
saladEnabled = true;
soup.setForeground(Color.lightGray);
salad.setForeground(Color.BLACK);
}
cream.setEnabled(soupEnabled);
broth.setEnabled(soupEnabled);
gumbo.setEnabled(soupEnabled);
tossed.setEnabled(saladEnabled);
caesar.setEnabled(saladEnabled);
croutons.setEnabled(saladEnabled);
lite.setEnabled(saladEnabled);
}
Label
label, label2, label3;
Panel
appetizerPanel, soupPanel, saladPanel, entreePanel, desertPanel, caloriesPanel;
Checkbox
standard, deluxe,
soup, salad,
crepe, quiche, dumpling,
cream, broth, gumbo,
tossed, caesar,
croutons, lite,
chicken, beef, lamb, fish,
pie, fruit, sherbet;
Button calories;
int total = 0;
TextField
textField, textField2, textField3,
textField4, textField5, textField6,
textField7, textField8, textField9,
textField10, textField11, textField12,
textField13, textField14, textField15,
textField16, textField17, calTextField;
}
In Java, you are not allowed to refer to a field a in an initializer of another field b if b is declared before a in the source code. That is, this is allowed:
int b = 3;
int a = b;
but this is not allowed:
int a = b;
int b = 3;
By the way, if Java had some magic way of figuring out that textField had to be initialized before crepeVal, you would still get a NullPointerException since textField is null after object construction.

GRADING SYSTEM using JComboBox for the entry of grades; Jlabel, JTextFirld,JFrame; JButton for events; all in JFrame

here is my code for our finals. a grading system were the grades are all in 24 JComboBox. some with numbers till 10, 20 and 30. each quarter they are computed and shown in a JTextfield. when the four quarters are complete a Jbutton "COMPUTE ALL" calculates the entire data and will show in another frame the student's grade and weather he or she have passed or failed. this code works good with some problems: (1) my Jbutton "CLEAR ALL" doesn't work. it should show the default number "0" when clicked and the Jtextfields must be empty-(THIS ALREADY WORKS "TEXTFIELD TO EMPTY". (2) i want to hide the second frame when the Jbutton for compute all is clicked (f2.hide();f3.show();) but it keeps saying (cannot find symbol
symbol: variable f2cannot find symbol) (3) and lastly in my 3rd frame where the student's final grade and final rating are shown i cant really have it shown in a message dialog stating weather he or she passed or failed. THANKS. sorry for the poor coding.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
JFrame f2 = new JFrame (x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
double grade, equiv;
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "" + equiv);
if (equi >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
' else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
//this part doesn't work. the JComboBox still contains the initialized number even though the "CLEAR ALL" button is clicked
cb1.setSelectedItem("0");
cb2.setSelectedItem("0");
cb3.setSelectedItem("0");
cb4.setSelectedItem("0");
cb5.setSelectedItem("0");
cb6.setSelectedItem("0");
cb7.setSelectedItem("0");
cb8.setSelectedItem("0");
cb9.setSelectedItem("0");
cb10.setSelectedItem("0");
cb11.setSelectedItem("0");
cb12.setSelectedItem("0");
cb13.setSelectedItem("0");
cb14.setSelectedItem("0");
cb15.setSelectedItem("0");
cb16.setSelectedItem("0");
cb17.setSelectedItem("0");
cb18.setSelectedItem("0");
cb19.setSelectedItem("0");
cb20.setSelectedItem("0");
cb21.setSelectedItem("0");
cb22.setSelectedItem("0");
cb23.setSelectedItem("0");
cb24.setSelectedItem("0");
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
Here is the complete corrected code,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JFrame f2 = new JFrame();
double grade, equiv;
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
f2.setTitle(x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "" + equiv);
if (equiv >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
cb1.setSelectedItem(Integer.valueOf(0));
cb2.setSelectedItem(Integer.valueOf(0));
cb3.setSelectedItem(Integer.valueOf(0));
cb4.setSelectedItem(Integer.valueOf(0));
cb5.setSelectedItem(Integer.valueOf(0));
cb6.setSelectedItem(Integer.valueOf(0));
cb7.setSelectedItem(Integer.valueOf(0));
cb8.setSelectedItem(Integer.valueOf(0));
cb9.setSelectedItem(Integer.valueOf(0));
cb10.setSelectedItem(Integer.valueOf(0));
cb11.setSelectedItem(Integer.valueOf(0));
cb12.setSelectedItem(Integer.valueOf(0));
cb13.setSelectedItem(Integer.valueOf(0));
cb14.setSelectedItem(Integer.valueOf(0));
cb15.setSelectedItem(Integer.valueOf(0));
cb16.setSelectedItem(Integer.valueOf(0));
cb17.setSelectedItem(Integer.valueOf(0));
cb18.setSelectedItem(Integer.valueOf(0));
cb19.setSelectedItem(Integer.valueOf(0));
cb20.setSelectedItem(Integer.valueOf(0));
cb21.setSelectedItem(Integer.valueOf(0));
cb22.setSelectedItem(Integer.valueOf(0));
cb23.setSelectedItem(Integer.valueOf(0));
cb24.setSelectedItem(Integer.valueOf(0));
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
These were the problems,
1.When you give zero in quotes like "0" its taken as string, whereas Integer is what required.
2.The jFrame f2 was not accessible because it was defined inside another action event.
3.The same was the problem with the variable equiv, thats why it was not accessible in the event of jOptionpane.

Write to text file through a java GUI

I have created a java GUI that works as a 6-question questionnaire. For each of the six question, I have 4 choices , i, ii, iii, iv for the user to choose, and then type out one of the four choices in a text box, then click a button "enter" to go to the next question. After all six questions have been answered, it will jump to a finish page.
I want to write the input value (in the text box) of each question to a text file. So After clicking on the "enter button" to answer all six questions I can see something like "i ii iii i ii iii" in a text file.
Is there a way to do it?
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HTML extends Applet implements ActionListener
{
private TextField question;
private Button enter, start;
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String text, input;
private Label intro1, intro2;
private Label qone1, qone2, qone3, qone4, qone5, qone6;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
private Label finish1, finish2, finish3;
public void init()
{
setLayout(null);
start = new Button ("Start");
question = new TextField(10);
enter = new Button ("Enter");
if (count == 0)
{
setBackground( Color.yellow);
intro1 = new Label("Target Advertising", Label.CENTER);
intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
intro2 = new Label("Welcome to this questionnaire. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
add(intro1);
add(intro2);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,800,20);
add(start);
start.setBounds(370,60,70,23);
start.addActionListener(this);
}
if(count == 1)
{
setBackground( Color.yellow );
qone1 = new Label("Question 1", Label.LEFT);
qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qone2 = new Label("On average, How many hours do you spend on playing sports every week?");
qone3 = new Label("i.0-2");
qone4 = new Label("ii.3-6");
qone5 = new Label("iii.7-10");
qone6 = new Label("iv.10+");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
qone1.setBounds(15,0,800,20);
qone2.setBounds(15,20,800,15);
qone3.setBounds(15,60,800,15);
qone4.setBounds(15,80,800,15);
qone5.setBounds(15,100,800,15);
qone6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if (count == 2)
{
qtwo1 = new Label("Question 2", Label.LEFT);
qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qtwo2 = new Label("On average, How many hours do you spend on qsixening to music every week?");
qtwo3 = new Label("i. 0-4 ");
qtwo4 = new Label("ii. 5-10");
qtwo5 = new Label("iii. 11-20");
qtwo6 = new Label("iv. 20+");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
qtwo1.setBounds(15,20,800,15);
qtwo2.setBounds(15,40,800,15);
qtwo3.setBounds(15,60,800,15);
qtwo4.setBounds(15,80,800,15);
qtwo5.setBounds(15,100,800,15);
qtwo6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 3)
{
qthree1 = new Label("Question 3", Label.LEFT);
qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qthree2 = new Label("On average, How many hours do you spend on using computers every week?");
qthree3 = new Label("i.0-2");
qthree4 = new Label("ii.3-10");
qthree5 = new Label("iii.11-15");
qthree6 = new Label("iv.20+");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
qthree1.setBounds(15,20,800,20);
qthree2.setBounds(15,40,800,15);
qthree3.setBounds(15,60,800,15);
qthree4.setBounds(15,80,800,15);
qthree5.setBounds(15,100,800,15);
qthree6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 4)
{
qfour1 = new Label("Question 4", Label.LEFT);
qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfour2 = new Label("On average, How many hours do you spend on groceries every week?");
qfour3 = new Label("i.0-2");
qfour4 = new Label("ii.3-10");
qfour5 = new Label("iii.11-15");
qfour6 = new Label("iv.20+");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
qfour1.setBounds(15,20,800,15);
qfour2.setBounds(15,40,800,15);
qfour3.setBounds(15,60,800,15);
qfour4.setBounds(15,80,800,15);
qfour5.setBounds(15,100,800,15);
qfour6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 5)
{
qfive1 = new Label("Question 5", Label.LEFT);
qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfive2 = new Label("On average, How many hours do you spend on watching TV every week?");
qfive3 = new Label("i.0-2");
qfive4 = new Label("ii.3-10");
qfive5 = new Label("iii.11-15");
qfive6 = new Label("iv.20+");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
qfive1.setBounds(15,20,800,15);
qfive2.setBounds(15,40,800,15);
qfive3.setBounds(15,60,800,15);
qfive4.setBounds(15,80,800,15);
qfive5.setBounds(15,100,800,15);
qfive6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 6)
{
qsix1 = new Label("Question 6", Label.LEFT);
qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qsix2 = new Label("On average, How many times do you spend with family every week?");
qsix3 = new Label("i.0-2");
qsix4 = new Label("ii.3-10");
qsix5 = new Label("iii.11-15");
qsix6 = new Label("iv.20+");
add(qsix1);
add(qsix2);
add(qsix3);
add(qsix4);
add(qsix5);
add(qsix6);
qsix1.setBounds(15,20,800,15);
qsix2.setBounds(15,40,800,15);
qsix3.setBounds(15,60,800,15);
qsix4.setBounds(15,80,800,15);
qsix5.setBounds(15,100,800,15);
qsix6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
finish1 = new Label("Thank You." , Label.CENTER);
finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
finish2 = new Label("Questionnaire Completed.", Label.CENTER);
finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));
add(finish1);
add(finish2);
finish1.setBounds(0,200,800,60);
finish2.setBounds(0,300,800,60);
}
}
public void actionPerformed(ActionEvent ae)
{
String button = ae.getActionCommand();
text = question.getText();
b = 0;
c = 0;
if (count == 6)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qsix1);
remove(qsix2);
remove(qsix3);
remove(qsix4);
remove(qsix5);
remove(qsix6);
if(input.equals("OL"))
{
b = 1;
count = 7;
init();
}
else
{
b = 2;
count = 7;
init();
}
}
if (count == 5)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qfive1);
remove(qfive2);
remove(qfive3);
remove(qfive4);
remove(qfive5);
remove(qfive6);
if(input.equals("BR"))
{
b = 1;
count = 6;
init();
}
else
{
b = 2;
count = 6;
init();
}
}
if (count == 4)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qfour1);
remove(qfour2);
remove(qfour3);
remove(qfour4);
remove(qfour5);
remove(qfour6);
}
if(input.equals("no"))
{
b = 1;
count = 5;
init();
}
else
{
b = 2;
count = 5;
init();
}
}
if (count == 3)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qthree1);
remove(qthree2);
remove(qthree3);
remove(qthree4);
remove(qthree5);
remove(qthree6);
if(input.equals("black"))
{
b = 1;
count = 4;
init();
}
else
{
b = 2;
count = 4;
init();
}
}
if (count == 2)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qtwo1);
remove(qtwo2);
remove(qtwo3);
remove(qtwo4);
remove(qtwo5);
remove(qtwo6);
if(input.equals("yes"))
{
b = 1;
count = 3;
init();
}
else
{
b = 2;
count = 3;
init();
}
}
if (count == 1)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qone1);
remove(qone2);
remove(qone3);
remove(qone4);
remove(qone5);
remove(qone6);
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 1;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(start);
count = 1;
init();
}
this.validate();
}
}
Sure, something like
OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();

Categories