1

I am trying to create an action listener that will pull variable information from outside the action listener class, and using that data, will call a method to do a calculation. After that, I want the action listener to update my pane with the results of the calculations. Unfortunately, I am getting the "Cannot find symbol error" on some of my variables in my CalculateButtonHandler section below. Can someone please explain why this is happening? I've marked where the errors are occurring.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;



 public class DayCalculator extends JFrame 
  {

  //declaring variables
  private JTextField daysEntry, dEntry; 
  private CalculateButtonHandler cbHandler; 
  private ExitButtonHandler ebHandler;

 public DayCalculator ()
  {
     setTitle("Day Calculator");
     setSize (600, 350);

     //getting current day name in String format
     final String weekdayName = new SimpleDateFormat
     ("EEEE", Locale.ENGLISH).format(System.currentTimeMillis());     

     JPanel listPane = new JPanel();                        
     listPane.setPreferredSize(new Dimension (450, 250));
     listPane.setLocation(200,100);

     Container pane = getContentPane();  //creating and adding to content pane
     pane.setLayout(new GridBagLayout());


     JLabel instructions = new JLabel("Please enter number of days in" +
     " the past or future you'd like to know the day of.");

     JLabel todaysDate = new JLabel("Today is " + weekdayName +".");

     JTextField dEntry = new JTextField(10);


     JButton calculate = new JButton("Calculate"); //creating calculate button
     cbHandler = new CalculateButtonHandler();     //creating button listener
     calculate.addActionListener(cbHandler);       //instantiating listener


     JButton exit = new JButton ("Exit");          //creating Exit button
     ebHandler = new ExitButtonHandler();          //creating button listener
     exit.addActionListener(ebHandler);


     listPane.add(todaysDate);
     listPane.add(instructions);
     listPane.add(dEntry);        
     listPane.add(calculate);
     listPane.add(exit);

     pane.add(listPane);

     setDefaultCloseOperation(EXIT_ON_CLOSE);
     setVisible(true);                              //making JFrame visible

 }

  private class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
  {

     int daysEntered = Integer.parseInt(dEntry.getText());    //getting user entry  
     int dayNumber = DayResult(daysEntered, weekdayName);     ////CANNOT FIND SYMBOL ERROR on weekdayName

     JLabel results = new JLabel("Your selected day is" + dayNumber +".");   //adding result to pane
     listpane.add(results);  //CANNOT FIND SYMBOL ERROR
     pane.repaint();         //CANNOT FIND SYMBOL ERROR                                             

   }
}


  private class ExitButtonHandler implements ActionListener        
 {
    public void actionPerformed (ActionEvent e)
  {

     System.exit(0);
  }

}


public static void main(String[] args) { 

  DayCalculator dCalc = new DayCalculator(); 

 }


 public static String DayResult(int entry, String name)    //method to calculate new day based on user entry
    {

     int number;

    if (name == "Monday")

      number = 1;

    else if (name == "Tuesday")

      number = 2;

    else if (name == "Wednesday")

      number = 3;

    else if (name == "Thursday")

      number = 4;

    else if (name == "Friday")

      number = 5;

    else if (name == "Saturday")

      number = 6;

    else if (name == "Sunday")

      number = 7;

     System.out.println(number); //testing  



    int dayResult = (entry+number)-7;

    if (dayResult <0){
     dayResult = -dayResult; 
     }
    String dayNameResult;

    switch (dayResult){

    case 1: dayNameResult = "Monday";
            break;
    case 2: dayNameResult = "Tuedsay";
            break;
    case 3: dayNameResult = "Wednesday";
            break;
    case 4: dayNameResult = "Thursday";
            break;
    case 5: dayNameResult = "Friday";
            break;
    case 6: dayNameResult = "Saturday"; 
            break;
    case 7: dayNameResult = "Sunday"; 
           break;
    }

     return (dayNameResult);
  }
}

Errors are:

   DayCalculator.java:69: error: cannot find symbol
     int dayNumber = DayResult(daysEntered, weekdayName);     
     symbol:   variable weekdayName
     location: class DayCalculator.CalculateButtonHandler

   DayCalculator.java:72: error: cannot find symbol
     listpane.add(results);
     ^
    symbol:   variable listpane
    location: class DayCalculator.CalculateButtonHandler

  DayCalculator.java:73: error: cannot find symbol
     pane.repaint();                                                       
     ^
    symbol:   variable pane
    location: class DayCalculator.CalculateButtonHandler
    3 errors
carchelhf
  • 51
  • 1
  • 3
  • Learn about variable scope as that's your major problem. The listpane variable for instance is declared inside if the DayCalculator constructor and is thus visible only within the constructor. – Hovercraft Full Of Eels Nov 13 '15 at 17:22
  • Reimeus, I know that it cannot find the variables, but I don't understand why it cannot find them. I also reviewed a lot of other posts on this site that seemed to be about similar issues, but most of those involved people not importing the ActionEvent or ActionListener packages, which was not my issue. – carchelhf Nov 13 '15 at 17:23

0 Answers0