Re: Method doesn't perform
Quote:
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);
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:
http://img180.imageshack.us/img180/5...ngwords6kp.jpg
Do I have to use something like tfanswer.getText in my second if-statement?
Re: Method doesn't perform
Yes, of course you have to.
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]);
}
}
}
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.
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?
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.
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 :)
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?
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.
Re: Method doesn't perform
Quote:
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.
Quote:
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.)
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])) {
^
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?
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.
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).
http://img403.imageshack.us/img403/2...ngwords8xa.jpg
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?