Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. cannot resolve

cannot resolve

Scheduled Pinned Locked Moved Java
javahelp
9 Posts 2 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MallardsReach
    wrote on last edited by
    #1

    Below is the full code I am using but I get the following error messages showing at the start of my code

    Quote:

    Multiple markers at this line - txtNumOne cannot be resolved - txtNumTwo cannot be resolved

    and

    Quote:

    lblDPAnswer cannot be resolved

    package components;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.BorderFactory;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class MathsLesson
    {
    String strAnswer;

    	void showAnswer(int dPlaces){
    Double numAnswer; String strAnswer; 
    numAnswer = (Double.parseDouble(txtNumOne.getText()))\* (Double.parseDouble(txtNumTwo.getText()));
    strAnswer = String.format("%,." + dPlaces + "f", numAnswer); 
    lblDPAnswer.setText(strAnswer);
    

    }

    public MathsLesson() 
    {		
    	JFrame frame = new JFrame("Tables"); // Creating instance of JFrame
        frame.setSize(800, 600); // Setting the width and height of frame
        frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); 
       	JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
    	panel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(255, 51, 51), new java.awt.Color(153, 0, 51)));
    	frame.add(panel); // Add panel to frame
       	frame.setVisible(true); // Setting the frame visibility to true
    		panel.setLayout(null);	
    	    JPanel tablepanel = new JPanel();
    		tablepanel.setLayout(null);
    		tablepanel.setBounds(10,20,320,300);
    		tablepanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Times Tables"));
    		tablepanel.setOpaque(false); 
    		panel.add(tablepanel);
    		
    		panel.setLayout(null);	
    	    JPanel decimalpanel = new JPanel();
    		decimalpanel.setLayout(null);
    		decimalpanel.setBounds(10,320,320,100);
    		decimalpanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Decimal Places"));
    		decimalpanel.setOpaque(false); 
    		panel.add(decimalpanel);
    		
    		JTextArea displayTable = new JTextArea();
    		displayTable.setEditable(false);
    		displayTable.setFont(new java.awt.Font("Times New Roman", 0, 18));
    		displayTable.setText("Choose a button on the right to view the table you want to see");
    		displayTable.setLineW
    
    L 1 Reply Last reply
    0
    • M MallardsReach

      Below is the full code I am using but I get the following error messages showing at the start of my code

      Quote:

      Multiple markers at this line - txtNumOne cannot be resolved - txtNumTwo cannot be resolved

      and

      Quote:

      lblDPAnswer cannot be resolved

      package components;
      import java.awt.event.*;
      import javax.swing.*;
      import java.awt.Color;
      import java.awt.Font;
      import javax.swing.BorderFactory;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      public class MathsLesson
      {
      String strAnswer;

      	void showAnswer(int dPlaces){
      Double numAnswer; String strAnswer; 
      numAnswer = (Double.parseDouble(txtNumOne.getText()))\* (Double.parseDouble(txtNumTwo.getText()));
      strAnswer = String.format("%,." + dPlaces + "f", numAnswer); 
      lblDPAnswer.setText(strAnswer);
      

      }

      public MathsLesson() 
      {		
      	JFrame frame = new JFrame("Tables"); // Creating instance of JFrame
          frame.setSize(800, 600); // Setting the width and height of frame
          frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); 
         	JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
      	panel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(255, 51, 51), new java.awt.Color(153, 0, 51)));
      	frame.add(panel); // Add panel to frame
         	frame.setVisible(true); // Setting the frame visibility to true
      		panel.setLayout(null);	
      	    JPanel tablepanel = new JPanel();
      		tablepanel.setLayout(null);
      		tablepanel.setBounds(10,20,320,300);
      		tablepanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Times Tables"));
      		tablepanel.setOpaque(false); 
      		panel.add(tablepanel);
      		
      		panel.setLayout(null);	
      	    JPanel decimalpanel = new JPanel();
      		decimalpanel.setLayout(null);
      		decimalpanel.setBounds(10,320,320,100);
      		decimalpanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Decimal Places"));
      		decimalpanel.setOpaque(false); 
      		panel.add(decimalpanel);
      		
      		JTextArea displayTable = new JTextArea();
      		displayTable.setEditable(false);
      		displayTable.setFont(new java.awt.Font("Times New Roman", 0, 18));
      		displayTable.setText("Choose a button on the right to view the table you want to see");
      		displayTable.setLineW
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are creating local variables in one method, and then trying to access them from other methods. You need to understand the scope rules, i.e whether variables are local or not. So txtNumOne and txtNumTwo should be class level variables. Alternatively they could be passed as parameters to the showAnswer method. You have a potential mistake in the following code:

        String strAnswer; // this is a class level variable, i.e. visible to every method in the class
         
      void showAnswer(int dPlaces){
          Double numAnswer;
          // this has the same name as the class level variable,
          // but is only visible inside this method, so a possible conflict
          String strAnswer;
          // don't write compound statements like this, they are much harder to diagnose when things go wrong
          numAnswer = (Double.parseDouble(txtNumOne.getText()))\* (Double.parseDouble(txtNumTwo.getText()));
          strAnswer = String.format("%,." + dPlaces + "f", numAnswer); 
          lblDPAnswer.setText(strAnswer);
      

      }

      M 1 Reply Last reply
      0
      • L Lost User

        You are creating local variables in one method, and then trying to access them from other methods. You need to understand the scope rules, i.e whether variables are local or not. So txtNumOne and txtNumTwo should be class level variables. Alternatively they could be passed as parameters to the showAnswer method. You have a potential mistake in the following code:

          String strAnswer; // this is a class level variable, i.e. visible to every method in the class
           
        void showAnswer(int dPlaces){
            Double numAnswer;
            // this has the same name as the class level variable,
            // but is only visible inside this method, so a possible conflict
            String strAnswer;
            // don't write compound statements like this, they are much harder to diagnose when things go wrong
            numAnswer = (Double.parseDouble(txtNumOne.getText()))\* (Double.parseDouble(txtNumTwo.getText()));
            strAnswer = String.format("%,." + dPlaces + "f", numAnswer); 
            lblDPAnswer.setText(strAnswer);
        

        }

        M Offline
        M Offline
        MallardsReach
        wrote on last edited by
        #3

        Thanks Richard after many hours of head scratching and internet searching I think I have it. Below is the class level variables and the method showAnswer.

        // Variables declaration                    
               double decNum1, decNum2, decAnswer; // class level available to all
               String strAnswer;
          // End of variables declaration 
        		
        void showAnswer(int dPlaces){
        	Double numAnswer; 
        	numAnswer = decNum1 \* decNum2;
        	strAnswer = String.format("%,." + dPlaces + "f", numAnswer);     	
        

        }

        And below is the 3 radiobuttons and the sum button that call the method when clicked.

        		      rbOneDecimal.addActionListener(new ActionListener() {
        			         public void actionPerformed(ActionEvent e) {
        			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
        			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
        			        	  showAnswer(1); //call method
        			        	  lblDPAnswer.setText(strAnswer);
        			      				        	}
        			      });
        		      rbTwoDecimal.addActionListener(new ActionListener() {
        			         public void actionPerformed(ActionEvent e) {
        			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
        			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
        			        	  showAnswer(2); //call method
        			        	  lblDPAnswer.setText(strAnswer); 
        			         }
        			      });
        		      
        		      rbThreeDecimal.addActionListener(new ActionListener() {
        			         public void actionPerformed(ActionEvent e) {   
        			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
        			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
        			        	  showAnswer(3); //call method
        			        	  lblDPAnswer.setText(strAnswer);
        			         }
        			      });
        		      btnSum.addActionListener(new ActionListener() {
        			         public void actionPerformed(ActionEvent e) {
        			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
        			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
        			        	  showAnswer(0); //call method
        			        	  lblDPAnswer.setText(strAnswer);
        			        	 }
        			      });
        

        Could it be done a better way? I seem to be repeating these 2 lines.

               decNum1 = Double.parseDouble(txtNumOne.getText());
               decNum2 = Double.parseDouble(txtNumTwo.getText());
        
        L 1 Reply Last reply
        0
        • M MallardsReach

          Thanks Richard after many hours of head scratching and internet searching I think I have it. Below is the class level variables and the method showAnswer.

          // Variables declaration                    
                 double decNum1, decNum2, decAnswer; // class level available to all
                 String strAnswer;
            // End of variables declaration 
          		
          void showAnswer(int dPlaces){
          	Double numAnswer; 
          	numAnswer = decNum1 \* decNum2;
          	strAnswer = String.format("%,." + dPlaces + "f", numAnswer);     	
          

          }

          And below is the 3 radiobuttons and the sum button that call the method when clicked.

          		      rbOneDecimal.addActionListener(new ActionListener() {
          			         public void actionPerformed(ActionEvent e) {
          			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
          			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
          			        	  showAnswer(1); //call method
          			        	  lblDPAnswer.setText(strAnswer);
          			      				        	}
          			      });
          		      rbTwoDecimal.addActionListener(new ActionListener() {
          			         public void actionPerformed(ActionEvent e) {
          			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
          			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
          			        	  showAnswer(2); //call method
          			        	  lblDPAnswer.setText(strAnswer); 
          			         }
          			      });
          		      
          		      rbThreeDecimal.addActionListener(new ActionListener() {
          			         public void actionPerformed(ActionEvent e) {   
          			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
          			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
          			        	  showAnswer(3); //call method
          			        	  lblDPAnswer.setText(strAnswer);
          			         }
          			      });
          		      btnSum.addActionListener(new ActionListener() {
          			         public void actionPerformed(ActionEvent e) {
          			        	 decNum1 = Double.parseDouble(txtNumOne.getText());
          			        	 decNum2 = Double.parseDouble(txtNumTwo.getText());
          			        	  showAnswer(0); //call method
          			        	  lblDPAnswer.setText(strAnswer);
          			        	 }
          			      });
          

          Could it be done a better way? I seem to be repeating these 2 lines.

                 decNum1 = Double.parseDouble(txtNumOne.getText());
                 decNum2 = Double.parseDouble(txtNumTwo.getText());
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Rather than make these variables at class level, make them parameters to showAnswer (actually calcAnswer would be a better name) method. And the final result shoud be the return value. Something like:

          String showAnswer(double decNum1, double decNum2, int dPlaces){
          Double numAnswer = decNum1 * decNum2;
          String strAnswer = String.format("%,." + dPlaces + "f", numAnswer);

          return strAnswer;
          

          }

          That keeps everything neatly within the method. You can reduce all the duplication by using the same handler for all the buttons.

          public void actionPerformed(ActionEvent e) {
          int numDecimals = // get the number from the button text

          double decNum1 = Double.parseDouble(txtNumOne.getText());
          double decNum2 = Double.parseDouble(txtNumTwo.getText());
          String strResult = showAnswer(decNum1, decNum2, numDecimals); //call method
          lblDPAnswer.setText(strResult);
          

          }

          Then add that to each button:

          rbOneDecimal.addActionListener(actionPerformed);
          rbTwoDecimal.addActionListener(actionPerformed);

          You may need to check the syntax requirements for these last two/three items

          M 2 Replies Last reply
          0
          • L Lost User

            Rather than make these variables at class level, make them parameters to showAnswer (actually calcAnswer would be a better name) method. And the final result shoud be the return value. Something like:

            String showAnswer(double decNum1, double decNum2, int dPlaces){
            Double numAnswer = decNum1 * decNum2;
            String strAnswer = String.format("%,." + dPlaces + "f", numAnswer);

            return strAnswer;
            

            }

            That keeps everything neatly within the method. You can reduce all the duplication by using the same handler for all the buttons.

            public void actionPerformed(ActionEvent e) {
            int numDecimals = // get the number from the button text

            double decNum1 = Double.parseDouble(txtNumOne.getText());
            double decNum2 = Double.parseDouble(txtNumTwo.getText());
            String strResult = showAnswer(decNum1, decNum2, numDecimals); //call method
            lblDPAnswer.setText(strResult);
            

            }

            Then add that to each button:

            rbOneDecimal.addActionListener(actionPerformed);
            rbTwoDecimal.addActionListener(actionPerformed);

            You may need to check the syntax requirements for these last two/three items

            M Offline
            M Offline
            MallardsReach
            wrote on last edited by
            #5

            So, each button calls the action performed method, which in turn calls the calcAnswer method, which then returns the answer to the action performed method where it is shown in the label? I'll go and place it in my project and see how I get on.(I could be back) ;)

            1 Reply Last reply
            0
            • L Lost User

              Rather than make these variables at class level, make them parameters to showAnswer (actually calcAnswer would be a better name) method. And the final result shoud be the return value. Something like:

              String showAnswer(double decNum1, double decNum2, int dPlaces){
              Double numAnswer = decNum1 * decNum2;
              String strAnswer = String.format("%,." + dPlaces + "f", numAnswer);

              return strAnswer;
              

              }

              That keeps everything neatly within the method. You can reduce all the duplication by using the same handler for all the buttons.

              public void actionPerformed(ActionEvent e) {
              int numDecimals = // get the number from the button text

              double decNum1 = Double.parseDouble(txtNumOne.getText());
              double decNum2 = Double.parseDouble(txtNumTwo.getText());
              String strResult = showAnswer(decNum1, decNum2, numDecimals); //call method
              lblDPAnswer.setText(strResult);
              

              }

              Then add that to each button:

              rbOneDecimal.addActionListener(actionPerformed);
              rbTwoDecimal.addActionListener(actionPerformed);

              You may need to check the syntax requirements for these last two/three items

              M Offline
              M Offline
              MallardsReach
              wrote on last edited by
              #6

              Thought it wouldn't work as easy as that. :( errors for the actionPerformed method,

              Quote:

              Multiple markers at this line - void is an invalid type for the variable actionPerformed - Syntax error on token "(", ; expected - Syntax error on token ")", ; expected

              errors for all the buttons .addActionListener(ActionPerformed);

              Quote:

              ActionPerformed cannot be resolved to a variable

              L 2 Replies Last reply
              0
              • M MallardsReach

                Thought it wouldn't work as easy as that. :( errors for the actionPerformed method,

                Quote:

                Multiple markers at this line - void is an invalid type for the variable actionPerformed - Syntax error on token "(", ; expected - Syntax error on token ")", ; expected

                errors for all the buttons .addActionListener(ActionPerformed);

                Quote:

                ActionPerformed cannot be resolved to a variable

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I hesitate to say this yet again, but please go to The Java™ Tutorials[^] and study the language properly. You are not going to learn it piecemeal by posting questions here, as each issue is looked at in isolation. You need to follow a structured learning path to get a full understanding of, not just the language, but its framework support as well.

                1 Reply Last reply
                0
                • M MallardsReach

                  Thought it wouldn't work as easy as that. :( errors for the actionPerformed method,

                  Quote:

                  Multiple markers at this line - void is an invalid type for the variable actionPerformed - Syntax error on token "(", ; expected - Syntax error on token ")", ; expected

                  errors for all the buttons .addActionListener(ActionPerformed);

                  Quote:

                  ActionPerformed cannot be resolved to a variable

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Introduction to Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)[^]

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    Introduction to Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)[^]

                    M Offline
                    M Offline
                    MallardsReach
                    wrote on last edited by
                    #9

                    Thanks for all the help, will go and look at the link that you have given. Just realised all the examples can be downloaded and run in netbeans or using notepad++ / eclipse. So not only can you read the tutorials you can run and walk through them. I think I made the fundamental mistake of looking at various forums and java related sites picking up bits of code from here and there, only half of which I understand. So I have deleted all links, and will now go and read the tutorials. See you in about 6months time.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups