1 Attachment(s)
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;
Code Java:
//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
Code 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);
}
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.
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
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.
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.
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:
Code :
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.
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
Code JAVA:
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
Code java:
JLabel results = new JLabel("You answered " + Window2.countCorrectAnswers + " out of 5 correct. That is a ###%.");
and it worked! Thank you!!!