how to store into array.char cannot dereference
im encounter a problem on how to store the value generated by random function into an array.it requires me not to repeat asking the same alphabets in the question..The question is like this:
Write a graphical user interface (GUI) application to be used to test Kindergarten children on the alphabet (‘A’-‘Z’).
The application will ask questions like:
What comes after D?
A child is expected to answer the question and the application will inform the child if the answer is correct or wrong. The application will ask a total of 20 questions. After all 20 questions have been answered, the application will display a score (from 0 to 20) to inform the child how many questions he or she answered correctly. The application will also list all the letters that were asked.
The letters tested are from ‘A’ up to ‘Y’. Do not include the letter ‘Z’. The letters are selected randomly. No letters are to be repeated, that is, each letter is only asked once in the 20 questions.
my code is as below:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class lol1 extends JFrame implements ActionListener
{
// private JLabel lblQuesNo;
private JLabel lblPromptQues;
private JLabel lblDisplayLetter;
private JLabel lblAnswer;
private JLabel lblCorrectWrong;
private JButton btnClear;
private JButton btnSubmit;
private JTextField txtInput;
//Array to hold the preload letter
private char [] storeLetter = new char[25];
//Hold the letter each time it is created
private char letter;
//Array to hold letter generated
//private char [] genLetter = new char[25];
private int count=0;
private int score=0;
private int j;
private int i;
public lol1(){
setSize(220,220);
setLocation(300,500);
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
if(storeLetter[i]==0){
storeLetter[i] = letter;
i++;
for(i=0; i<storeLetter[i].length(); i++){
if (storeLetter[i] == letter)
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
}
}
//lblQuesNo = new JLabel("Question No");
lblPromptQues = new JLabel("What letter comes after ");
lblDisplayLetter = new JLabel(Character.toString(letter) + "?");
txtInput=new JTextField(5);
Container c;
JPanel p1;
JPanel p2;
JPanel p3;
p1 = new JPanel();
p1.setLayout(new FlowLayout());
//p1.add(lblQuesNo);
p1.add(lblPromptQues);
p1.add(lblDisplayLetter);
p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JLabel("Answer :"));
p2.add(txtInput);
lblCorrectWrong = new JLabel();
p3 = new JPanel();
p3.add(btnSubmit);
p3.add(btnClear);
p3.add(lblCorrectWrong);
c = getContentPane();
c.setLayout(new GridLayout(3,1));
c.add(p1, BorderLayout.NORTH);
c.add(p2, BorderLayout.CENTER);
c.add(p3, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
JButton clickedButton = (JButton)e.getSource();
String buttonText = clickedButton.getText();
if(buttonText.equals("Submit")){
if(txtInput.getText().length() <1)
{
lblCorrectWrong.setText("Input is blank.Key in your answer:");
}else if(txtInput.getText().length() >0 && count<20){
count++;
while(j<=count){
if(letter==storeLetter[j])
break;
j++;
}
if(Character.toUpperCase(txtInput.getText().charAt(0))==letter+1){
score++;
lblCorrectWrong.setText("Answer is correct!");
}else{
lblCorrectWrong.setText("Answer is incorrect!");
}
if(count>=20){
lblCorrectWrong.setText("Your score is " + Integer.toString(score) + " out of " + "20");
}
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
lblDisplayLetter.setText(Character.toString(letter) + "?");
}
}
if(buttonText.equals("Clear"))
txtInput.setText("");
}
public static void main(String[] args){
lol1 gui = new lol1();
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
just a lil help to solve..would rather stick to array
hmm..i think i would rather stick to array since im not used to Collection method.
by the way, why this line of code not works?
Code:
if(storeLetter[i]==0){
storeLetter[i] = letter;
i++;
for(i=0; i<storeLetter[i].length(); i++){
if (storeLetter[i] == letter)
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
}
}
my problem is to store into array and make no repeat of the alphabets
if i can store it and no repeat then i will get my thing done :)
in confusion..i will learn the collection soon but not now.=).it is interesting
here is some refinement but the problem still occurs.
<<char cannot be dereferenced>>
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class lol extends JFrame implements ActionListener
{
private JLabel lblPromptQues;
private JLabel lblDisplayLetter;
private JLabel lblAnswer;
private JLabel lblCorrectWrong;
private JButton btnClear;
private JButton btnSubmit;
private JTextField txtInput;
//Array to hold the letter generated
private char [] storeLetter = new char[25];
//Hold the letter each time it is created
private char letter;
private int count=0;
private int score=0;
private int j;
private int i;
public lol(){
setSize(220,220);
setLocation(300,500);
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
lblPromptQues = new JLabel("What letter comes after ");
lblDisplayLetter = new JLabel(Character.toString(letter) + "?");
txtInput=new JTextField(5);
Container c;
JPanel p1;
JPanel p2;
JPanel p3;
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lblPromptQues);
p1.add(lblDisplayLetter);
p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JLabel("Answer :"));
p2.add(txtInput);
lblCorrectWrong = new JLabel();
p3 = new JPanel();
p3.add(btnSubmit);
p3.add(btnClear);
p3.add(lblCorrectWrong);
c = getContentPane();
c.setLayout(new GridLayout(3,1));
c.add(p1, BorderLayout.NORTH);
c.add(p2, BorderLayout.CENTER);
c.add(p3, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
JButton clickedButton = (JButton)e.getSource();
String buttonText = clickedButton.getText();
//perform verification if "Submit" button is entered
if(buttonText.equals("Submit")){
//check if there is any input
if(txtInput.getText().length() <1){
lblCorrectWrong.setText("Input is blank.Key in your answer:");
//check if there is input and question asked is less than 20
}else if(txtInput.getText().length() >0 && count<20){
//check if input is integer or character
if(Character.isLetter(txtInput.getText().charAt(0))){
count++;
while(j<=count){
if(letter==storeLetter[j])
break;
j++;
}
if(storeLetter[i]==0){
storeLetter[i] = letter;
i++;
for(i=0; i<storeLetter[i].length(); i++){
if (storeLetter[i] == letter)
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
}
}
//converting lowercase to uppercase, check if input is correct
if(Character.toUpperCase(txtInput.getText().charAt(0))==letter+1){
score++;
lblCorrectWrong.setForeground(Color.red);
lblCorrectWrong.setText("Answer is correct!");
}else{
lblCorrectWrong.setForeground(Color.red);
lblCorrectWrong.setText("Answer is incorrect!");
}
//display output when total question asked is 20
if(count>=20){
lblCorrectWrong.setText("Your score is " + Integer.toString(score) + " out of " + "20");
}
letter = (char)((int)'A'+Math.random()*((int)'Y'-(int)'A'+1));
lblDisplayLetter.setText(Character.toString(letter) + "?");
}else{
lblCorrectWrong.setForeground(Color.red);
lblCorrectWrong.setText("Input must be a character");
txtInput.setText("");
}
}
}
//clear the textbox if "clear" button is entered
if(buttonText.equals("Clear"))
txtInput.setText("");
}
public static void main(String[] args){
lol gui = new lol();
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}