|
-
Jan 19th, 2007, 08:02 AM
#1
Thread Starter
New Member
Method doesn't perform
Hi all,
i tried to make an application, which would simplify the proces of learning words.. I'm dutch, so I sometimes have to learn translations from english to dutch and viceversa.
This is my application. I got rid of the compiling-errors, but the method setQuestion doesn't perform (see the picture).
Who can help?
Code:
class Main {
public static void main(String[] arg) {
Words words = new Words ();
words.setQuestion();
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Words extends JFrame implements ActionListener {
private JTextField tfanswer;
private JLabel lquestion;
private JLabel lempty;
private JLabel lanswer;
private JButton bsubmit;
private JButton bnext;
private JButton bstop;
private Random r = new Random();
private int i;
public Words() {
//Making the textfields, labels and buttons
tfanswer = new JTextField(20);
lquestion = new JLabel("", JLabel.CENTER);
lempty = new JLabel("", JLabel.CENTER);
lanswer = new JLabel("", JLabel.CENTER);
bsubmit = new JButton("Submit");
bnext = new JButton("Next");
bstop = new JButton("Stop");
bnext.addActionListener(this);
bsubmit.addActionListener(this);
bstop.addActionListener(this);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 2));
p1.add(lquestion);
p1.add(tfanswer);
p1.add(lempty);
p1.add(lanswer);
JPanel p2 = new JPanel();
p2.add(bsubmit);
p2.add(bnext);
p2.add(bstop);
Container c = getContentPane();
c.add(p1, BorderLayout.CENTER);
c.add(p2, BorderLayout.SOUTH);
setTitle("Learning Words");
setSize(400,150);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
String[] question = {
"question1",
"question2",
"question3",
};
String[] answer = {
"answer1",
"answer2",
"answer3"
};
public void setQuestion() {
lquestion.setText(question[0]);
}
public void nextQuestion() {
i = r.nextInt(3);
lquestion.setText(question[i]);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Submit")) {
String temp = tfanswer.getText();
if (temp.equals(answer[i])) {
lanswer.setText("Correct!");
}
else {
lanswer.setText("Wrong, the answer is " + answer[0]);
}
}
else if (e.getActionCommand().equals("Next")) {
this.nextQuestion();
}
else if (e.getActionCommand().equals("Stop")) {
System.exit(0);
}
}
}
Last edited by Michail; Jan 22nd, 2007 at 09:46 AM.
-
Jan 19th, 2007, 11:09 PM
#2
Addicted Member
Re: Method doesn't perform
private JTextField tfanswer;
private JLabel lquestion;
private JLabel lempty;
private JLabel lanswer;
private JButton bsubmit;
public Words() {
JTextField tfanswer = new JTextField(20);
JLabel lquestion = new JLabel();
JLabel lempty = new JLabel();
JLabel lanswer = new JLabel();
JButton bsubmit = new JButton("Submit");
bsubmit.addActionListener(this);
you sort of double declared the variables. you only need to declare them at the top where the private statements are, after they have declared there you don't need the objecct name (i.e. JLabel) infront of them in the constructor.
simply remove them, like this:
Code:
private JTextField tfanswer;
private JLabel lquestion;
private JLabel lempty;
private JLabel lanswer;
private JButton bsubmit;
public Words() {
tfanswer = new JTextField(20);
lquestion = new JLabel();
lempty = new JLabel();
lanswer = new JLabel();
bsubmit = new JButton("Submit");
bsubmit.addActionListener(this);
-
Jan 20th, 2007, 08:06 AM
#3
Thread Starter
New Member
Re: Method doesn't perform
Thank you! Stupid mistake of me ofcourse. But now another problem occurs. The method setQuestion performs normally, but when I give the matching answer, it still says it's the wrong answer. Take a look at the picture to see what I mean:

Do I have to use something like tfanswer.getText in my second if-statement?
-
Jan 20th, 2007, 08:11 AM
#4
Re: Method doesn't perform
Yes, of course you have to.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 20th, 2007, 08:24 AM
#5
Thread Starter
New Member
Re: Method doesn't perform
I tried something like this, but that doesn't seem to work either.
Code:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Submit")) {
String temp = tfanswer.getText();
if (lquestion.equals(question[0]) && temp.equals(answer[0])) {
lanswer.setText("Correct!");
}
else {
lanswer.setText("Wrong, the answer is " + answer[0]);
}
}
}
-
Jan 20th, 2007, 08:28 AM
#6
Re: Method doesn't perform
Try writing out the text in the box exactly (wrapped in something so you can detect whitespace). Manually check if there's any difference. It should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 20th, 2007, 08:34 AM
#7
Thread Starter
New Member
Re: Method doesn't perform
Sorry, but I don't know what you mean. I filled in the correct text in the textfield. In what should I wrap it?
-
Jan 20th, 2007, 08:36 AM
#8
Re: Method doesn't perform
Something like
System.out.println("'" + temp + "'");
and then check if the answer you gave is REALLY exactly the same as the one hardcoded into the code.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 20th, 2007, 08:59 AM
#9
Thread Starter
New Member
Re: Method doesn't perform
I inserted System.out.println("'" + temp + "'"); in the code, and the cmd-window shows that my answer is the same as the answer in the array. Weird hehe
-
Jan 20th, 2007, 09:20 AM
#10
Re: Method doesn't perform
Have you changed (or removed) the lquestion.equals(questions[0]) part of the expression?
Why do you check that, anyway?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 20th, 2007, 10:59 AM
#11
Thread Starter
New Member
Re: Method doesn't perform
Well, I thought that since the application has to verify that the answer given matches the question asked, I should check that the question in the label equals the question in the array.
Please correct me if I'm wrong, I've been studying java for only a few months now.
Edit: Thank you, I removed that part from the if-statement, and it works now!
Next step is to get the application to randomly ask questions from the array. But is that possible? Because if the questions are randomly picked, how can it find the matching answer in the other array?
Hm, maybe I can make a method nextQuestion(), in which the application picks a next random question. But then I still have the problem, how can it 'know' what answer matches what answer.
Last edited by Michail; Jan 20th, 2007 at 11:06 AM.
-
Jan 20th, 2007, 02:03 PM
#12
Re: Method doesn't perform
 Originally Posted by Michail
Well, I thought that since the application has to verify that the answer given matches the question asked, I should check that the question in the label equals the question in the array.
No. You set the question yourself, so you know which question you set. There's no need to verify it, as the user can't change it anyway.
Next step is to get the application to randomly ask questions from the array. But is that possible? Because if the questions are randomly picked, how can it find the matching answer in the other array?
Extremely simple. Look at your two arrays. You have n questions and n answers in the two arrays. answers[0] is the answer to questions[0], answers[1] is the answer to questions[1], and so on.
So what you need to do is pick a random index i in the range [0, n) (i.e. including 0, but excluding n), write the question questions[i] into the label and then compare against the answer answers[i].
For getting a random number check out java.util.Random. (java.lang.Math.random() also generates a random number, but it's of type double and thus inconvenient to use.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 22nd, 2007, 08:51 AM
#13
Thread Starter
New Member
Re: Method doesn't perform
I made a new method nextQuestion() but the compiler doesn't seem to like it:
Code:
public void nextQuestion() {
int i = nextInt(3);
lquestion.setText(question[i]);
}
And I put "private Random i = new Random();" where all the other private statements are.
Then I tried this:
Code:
public void nextQuestion() {
int i;
i.nextInt(3);
lquestion.setText(question[i]);
}
But that won't compile either.
It says this:
Code:
int cannot be dereferenced
i.nextInt(3);
^
and this:
Code:
incompatible types
found : java.util.Random
required: int
if (temp.equals(answer[i])) {
^
-
Jan 22nd, 2007, 09:25 AM
#14
Thread Starter
New Member
Re: Method doesn't perform
It works! See first post for the code 
Next step to put the questions/answers from the array in a .txt and read it out.
CornedBee, do you know how I can couple the Submit button to the return button on my keyboard?
Last edited by Michail; Jan 22nd, 2007 at 10:18 AM.
-
Jan 22nd, 2007, 12:56 PM
#15
Re: Method doesn't perform
Try looking for something like "default button". That's how it works in Windows. I don't know how it works in Java.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 22nd, 2007, 04:17 PM
#16
Thread Starter
New Member
Re: Method doesn't perform
Well that comes last then. 
Because it's ugly to have all the words in the code, I want the words in seperate .txt files. Also, I want the option to choose which chapter to learn in the gui. I made a nice JMenuBar to locate that option (see picture).

I now made a FileReader and a BufferedReader:
Code:
private FileReader filereader = new FileReader("chapter1.txt");
private BufferedReader buffreader = new BufferedReader(filereader);
And in actionPerformed:
Code:
else if (e.getSource() == chapter1Item) {
//change to chapter1.txt
}
But how can I couple the several .txt's to the matching chapterbutton in the menu? I have no idea yet what to put in that else if statement.
And if I make some .txt's with the words in them, how can I make arrays out of those .txt's?
Last edited by Michail; Jan 22nd, 2007 at 05:17 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|