Results 1 to 10 of 10
- 04-05-2010, 02:58 PM #1
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:
Java 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); } }
- 04-05-2010, 03:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Why don't you use a Collection, such as a List for those purposes; the following little method creates 20 unique (and randomly shuffled) letters and stores them in a List:
kind regards,Java Code:public static List<Character> createQuestions() { List<Character> q= new ArrayList<Character>(); for (char c= 'A'; c < 'Z'; q.add(c++)); Collections.shuffle(q); for (int i= 0; i < 5; q.remove(i++)); return q; }
Jos
- 04-05-2010, 04:44 PM #3
re:JosAh
thanks for suggesting the kind of method to me
but i never learn that before.if i want to use the method that u suggested,where shall i put it in?
- 04-05-2010, 05:09 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
First try to exercise that small method a bit:
This little class calls the method (which generates the questions) and the main method prints all the questions. See if it works for you, build your program step by step; babysteps.Java Code:public class Test { public static List<Character> createQuestions() { ... } public static void main(String[] args) { List<Character> questions= createQuestions(); System.out.println(questions); } }
kind regards,
Jos
- 04-05-2010, 05:37 PM #5
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?
Java 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 :)
- 04-05-2010, 05:47 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-05-2010, 06:23 PM #7
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>>
Java 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); } }
- 04-05-2010, 06:28 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-05-2010, 06:52 PM #9
highlighted error
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);
}
}
char cannot be dereferenceLast edited by dark_metal; 04-05-2010 at 07:00 PM. Reason: type of error not stated
- 04-05-2010, 07:40 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Your compile whines about this line:
storeLetter is an array of chars; storeLetter[i] is a single char and it doesn't have a field .length, i.e. you can't dereference it with the '.' member operator.Java Code:for(i=0; i<storeLetter[i].length(); i++){
kind regards,
Jos
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
array of char
By sinisab in forum New To JavaReplies: 9Last Post: 01-05-2010, 09:48 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
pop from stack and store to char?
By viligante8 in forum New To JavaReplies: 13Last Post: 11-02-2008, 02:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks