2 Replies - 1002 Views - Last Post: 02 April 2011 - 03:47 AM Rate Topic: -----

#1 matashoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-April 11

cannot find symbol error

Posted 02 April 2011 - 03:08 AM

I have searched other forums for a similar problem with no sucess. Before these errors i was recieving a else without a if statement so something with the if statement in the Actionperformed(ActionEvent e) is messed up or i am over thinking the problem. Any ideas/suggestions would be helpful.

The two errors are

AveragingGrades.java:124: cannot find symbol
symbol : variable mnuFileNewGrades
location: class AveragingGrades
if (mnuFileNewGrades == src) {
^
AveragingGrades.java:124: cannot find symbol
symbol : variable mnuFileExit
location: class AveragingGrades
} else if (mnuFileExit == src) {

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.text.*;
 import java.text.DecimalFormat;

 // Begin public class AveragingGrades extends JFrame implements ActionListener
 public class AveragingGrades extends JFrame implements ActionListener
 {

	 boolean firstRun = true;
	 int arrayLength;
	 double[] numbers = new double[50];

	 JTextPane displayPane = new JTextPane();
	 DecimalFormat format = new DecimalFormat("######.##");


	 //Constructor
	 public AveragingGrades()
	 {
		 super("Averaging Grades");
	 }

	 //Begin public Container createContentPane()
	 public Container createContentPane()
	 {
	 	JPanel mainPanel = new JPanel();
	 	mainPanel.setLayout(new FlowLayout());

	 	displayPane = addTextToTextPane();
	 	JScrollPane scrollPane = new JScrollPane(displayPane);
	 	scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	 	scrollPane.setWheelScrollingEnabled(true);
	 	scrollPane.setPreferredSize(new Dimension(190, 300));
	 	mainPanel.add(scrollPane);
	 	Container c = getContentPane();
	 	c.setLayout(new FlowLayout());
	 	c.add(mainPanel);

	 return c;
	 } //End public Container createContentPane()

	 //Begin public JTextPane addTextToTextPane()
	 public JTextPane addTextToTextPane()
	 {
	 Document doc = displayPane.getDocument();
	 try
	 {
	 //The following lines generate JTextPane attributes on-the-fly
	 //to simplify adding text to the JTextPane
	 StyleContext style = StyleContext.getDefaultStyleContext();
	 AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);
	 displayPane.setParagraphAttributes(set,false);

	 doc.remove(0,doc.getLength());

	 for(int lcv = 0; lcv < arrayLength; lcv++)
	 {doc.insertString(doc.getLength(), String.valueOf(numbers[lcv])+ "\n" ,set);}

	 if(firstRun)
	 System.out.println("Program Started.");
	 else
	 {double average = average();
	 doc.insertString(doc.getLength(),"Average: " + String.valueOf(format.format(average))+ "\n",set);}
	 firstRun = false;
	 }
	 catch(BadLocationException ble)
	 {System.err.println("Couldn't insert text.");
	 }

	 //End public JTextPane addTextToTextPane()
	 return displayPane;
	 }

	 // Begin public JMenuBar createMenuBar()
	 public JMenuBar createMenuBar()
	  {

	 // Create an instance of the menu
	 JMenuBar mnuBar = new JMenuBar();
	 setJMenuBar(mnuBar);

	 //Construct and add the File menu
	 JMenu mnuFile = new JMenu("File");
		mnuFile.setMnemonic(KeyEvent.VK_F);
		mnuFile.setDisplayedMnemonicIndex(0);
		mnuBar.add(mnuFile);

	 //Construct and add the menu command to add grades
	 JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Grades");
		mnuFileNewGrades.setMnemonic(KeyEvent.VK_W);
		mnuFileNewGrades.setDisplayedMnemonicIndex(0);
		mnuFile.add(mnuFileNewGrades);
		mnuFileNewGrades.setActionCommand("NewGrades");
		mnuFileNewGrades.addActionListener(this);

	 //Construct and add the Exit Command
	 JMenuItem mnuFileExit = new JMenuItem("Exit");
		mnuFileExit.setMnemonic(KeyEvent.VK_X);
		mnuFileExit.setDisplayedMnemonicIndex(1);
		mnuFile.add(mnuFileExit);
		mnuFileExit.setActionCommand("Exit");
		mnuFileExit.addActionListener(this);

	 return mnuBar;
	 } // End public JMenuBar createMenuBar()

	 // Begin public void actionPerformed(ActionEvent e)
	 public void actionPerformed(ActionEvent e)
	{
	 Object src = e.getSource();

   		if (mnuFileNewGrades == src) {

  		} else if (mnuFileExit == src) {
    		System.out.println("Program Terminated.");
    		System.exit(0);
		}

	 {
	 //Resets the array so no values get accidentally figured in
	 for(int lcv = 0 ; lcv < numbers.length; lcv++)
	 numbers[lcv] = 0.0;

	 double grade = 0;
	 int counter = 0;

	 //Continue inputting grades until the user enters the sentinel
	 //value -1, or the array is completely filled
	 while(grade != -1 && counter < 50)
	 {
	 try
	 {
	 grade = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Grade", "Enter Value",JOptionPane.INFORMATION_MESSAGE));numbers[counter] = grade;

	 if(grade != -1)
	 counter++;

	 }
	 catch(NumberFormatException nfe) //User enters characters or nothing at all
	 {
	 System.out.println("Number Format Exception");
	 	JOptionPane.showMessageDialog(null, "You have entered an invalid number. If you are " + "finished, enter -1.", "Error",
	 	JOptionPane.INFORMATION_MESSAGE);
	 }
	 catch(NullPointerException npe) //User clicks "Cancel" instead of entering -1
	 {
	 	System.out.println("Null Pointer Exception");
	 	JOptionPane.showMessageDialog(null, "If you are finished, enter -1.", "Error",JOptionPane.INFORMATION_MESSAGE);
	 }

	 	}

	 	arrayLength = counter;
	 	sort();
	 	addTextToTextPane();
	 	}


	 } // End public void actionPerformed(ActionEvent e)

	 //Begin public double average()
	 public double average()
	 {
	 double sum = 0 , average;
	 int totalPositiveCount = 0;

	 for(int lcv = 0 ; lcv < arrayLength; lcv++)
	 {
	 if(numbers[lcv] > 0)
	 {
	 sum += numbers[lcv];
	 totalPositiveCount++;
	 }
	  }
	 average = sum / (double) totalPositiveCount;

	 return average;
	 } //End public double average()


	 //Sorts the array using a bubble sort method
	 //Begin public void sort()
	 public void sort()
	 {
	 boolean change;

	 do
	 {
	 change = false;

	 for (int lcv = 0 ; lcv < arrayLength - 1 ; lcv++)
	 {
	 if(numbers[lcv] > numbers[lcv + 1])
	 {
	 swap(lcv);
	 change = true;
	 }
	 }

	 }while(change);
	 } //End public void sort


	 //Swaps two elements in the array
	 //Begin public void swap(int index)
	 public void swap(int index)
	 {
	 	double temp = numbers[index];
		numbers[index] = numbers[index + 1];
	 	numbers[index + 1] = temp;
	 } //End public void swap(int index)

		 //Begin public static void main(String[] args)
	 	public static void main(String[] args)
	 {
	 	AveragingGrades m = new AveragingGrades();
		 m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 m.setJMenuBar( m.createMenuBar());
		 m.setContentPane( m.createContentPane());
		 m.setSize(200, 375);
		 m.setVisible(true);
		 JOptionPane.showMessageDialog(null, "To enter grades select File >> Enter New Grades" +
		 "To end input, enter -1 in the grade value box.",
		 "Welcome", JOptionPane .INFORMATION_MESSAGE);
		 //End public static void main(String[] args)
	 }
}



Is This A Good Question/Topic? 0
  • +

Replies To: cannot find symbol error

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: cannot find symbol error

Posted 02 April 2011 - 03:35 AM

You declare mnuFileExit and mnuFileNewGrades as local variables in createMenuBar method. i.e they exist only in that method scope.
Then, you try to acceess them in a different method (actionPerformed) the compiler doesn't recognize these variables, and inform you about it as a 'cannot find symbol' error.
If you want to be able to access these variables within all the class scope, declare them as global variables so they will be visible to all methods\constrcutors in the class.
To declare a global variable, simply declare it outside any method\constructor, like you declared firstRun and arrayLength.
Was This Post Helpful? 1
  • +
  • -

#3 matashoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-April 11

Re: cannot find symbol error

Posted 02 April 2011 - 03:47 AM

Thank you very much. Ive been up all night working on this and figured it was something simple I missed or was not understanding.

This post has been edited by matashoe: 02 April 2011 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1