Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Send Int to Another Class Help

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Arizona
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Send Int to Another Class Help

    Hello!

    I'm attempting to increment the correct answers of radio buttons in Window2.java then I am trying to pass the final value to Window3.java to display it in a JLabel. I have attached the files through zip

    Window2.java
    In earlier code I have
    28. public int countCorrectAnswers;

    64. countCorrectAnswers = 0;

    	//create question 1 and it's radio buttons
    		JLabel question1 = new JLabel("What's the title of the short story?");
     
    		JPanel answers1 = new JPanel();
    		question1Option1 = new JRadioButton("Camping Trip"); 
    		question1Option2 = new JRadioButton("Camp Food");			//correct answer
    		question1Option3 = new JRadioButton("Cooking Made Easy");
    		question1Option4 = new JRadioButton("Food is Great when Camping");
     
    		question1Group = new ButtonGroup();
    		question1Group.add(question1Option1);
    		question1Group.add(question1Option2);
    		question1Group.add(question1Option3);
    		question1Group.add(question1Option4);
     
    		answers1.add(question1Option1);
    		answers1.add(question1Option2);	//correct answer
    		answers1.add(question1Option3);
    		answers1.add(question1Option4);
     
    	if (question1Option2.isSelected())
    		countCorrectAnswers++;

    The above occurs 5x for 5 questions



    Window3.java


    public class Window3 extends JFrame
    {
    	private JLabel results;
    	private JPanel buttonPanel;
    	private JButton button;
     
    	private JMenuBar menuBar;
    	private JMenu fileMenu;
    	private JMenu aboutMenu;
    	private JMenuItem exitItem;
    	private JMenuItem infoItem;
     
    	public Window3()
    	{
    	//	Set GUI information & layout; display window
     
    		setTitle("Results of Short Story & Quiz");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
     
    		buildMenuBar();
     
     
    	[B]// I want to access countCorrectAnswers from Window2.java in order to display in the following JLabel[/B]
     
    		JLabel results = new JLabel("You answered " + countCorrectAnswers + " out of 5 correct. That is a ###%.");
    		add(results, BorderLayout.CENTER);
     
    		buildButtonPanel();
    		add(button, BorderLayout.SOUTH);
     
    		pack();
    		setVisible(true);
    	}
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Send Int to Another Class Help

    Where is the variable: countCorrectAnswers defined? I don't see its definition in the posted code?

    Is there more than one variable defined with that name so that one shadows the other?
    One version is incremented and the other version is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Arizona
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Int to Another Class Help

    I have countCorrectAnswers defined and initialized in Window2.java

    No, Window2.java should be the only location where the variable is defined as a public int

    I need it printed in Window3.java

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Send Int to Another Class Help

    Do you get any compiler errors? Please copy the full text of a any error messages and paste it here.
    Window3 should NOT compile without an error if the variable: countCorrectAnswers is not defined in Window3.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Arizona
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Int to Another Class Help

    I receive the following compiler error:

    ----jGRASP exec: javac -g Window3.java

    Window3.java:29: error: cannot find symbol
    JLabel results = new JLabel("You answered " + countCorrectAnswers + " out of 5 correct. That is a ###%.");
    ^
    symbol: variable countCorrectAnswers
    location: class Window3
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Send Int to Another Class Help

    Did you have that error message when you made the first post? It's important to solving the problem.

    To access a variable or method that is defined in another class, you need a reference to that class:
    refToOtherClass.otherClassVariable

    Is there a reference to an instance of the Window2 class in the Window3 class? Use that reference with countCorrectAnswers to access it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    lancergiirl (May 5th, 2013)

  8. #7
    Junior Member
    Join Date
    May 2013
    Location
    Arizona
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Int to Another Class Help

    Yes I did get that error in the first post also.

    Okay, I have added refToOtherClass.otherClassVariable as


    Window2.countCorrectAnswers
     
    JLabel results = new JLabel("You answered " + countCorrectAnswers + " out of 5 correct. That is a ###%.");



    But now I am experiencing the following complier error:

    ----jGRASP exec: javac -g Window3.java

    Window3.java:31: error: ';' expected
    JLabel results = new JLabel("You answered " + countCorrectAnswers + " out of 5 correct. That is a ###%.");
    __ __^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.




    Thanks so much for your time by the way!

    --- Update ---

    Nevermind!!! I realized I misunderstood your post at first


    I tried

    JLabel results = new JLabel("You answered " + Window2.countCorrectAnswers + " out of 5 correct. That is a ###%.");


    and it worked! Thank you!!!
    Last edited by lancergiirl; May 5th, 2013 at 09:43 PM. Reason: removing extra word out of place

Similar Threads

  1. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  2. [SOLVED] how to send variable to different class by using extends
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 05:05 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. [jsp/jsf] Send string(file name) to other class
    By barni120 in forum JavaServer Pages: JSP & JSTL
    Replies: 13
    Last Post: February 10th, 2012, 07:48 AM
  5. How to call int value from another class
    By IDK12 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 14th, 2011, 09:18 PM

Tags for this Thread