Results 1 to 10 of 10
  1. #1
    dark_metal's Avatar
    dark_metal is offline Member
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    9
    Rep Power
    0

    Default 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);	
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    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:

    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;
    }
    kind regards,

    Jos

  3. #3
    dark_metal's Avatar
    dark_metal is offline Member
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    9
    Rep Power
    0

    Default 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?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dark_metal View Post
    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?
    First try to exercise that small method a bit:

    Java Code:
    public class Test {
       public static List<Character> createQuestions() { ... }
    
       public static void main(String[] args) {
          List<Character> questions= createQuestions();
          System.out.println(questions);
       }
    }
    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.

    kind regards,

    Jos

  5. #5
    dark_metal's Avatar
    dark_metal is offline Member
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    9
    Rep Power
    0

    Default 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 :)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dark_metal View Post
    by the way, why this line of code not works?
    Define "does not work". Doesn't it compile? Does it throw an Exception at runtime? Erroneous behaviour? Daemons flying out of your nose?

    kind regards,

    Jos

    ps. Collections are much more flexible than arrays. Try to study them. In the (near) future you'll need them.

  7. #7
    dark_metal's Avatar
    dark_metal is offline Member
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    9
    Rep Power
    0

    Default 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);	
    	}
    }

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dark_metal View Post
    here is some refinement but the problem still occurs.
    <<char cannot be dereferenced>>
    I guess it's a compilation error; why don't you mention the offending line? Programming and problem solving is not a guessing game; if you want help please help us to help you and be as clear and explicit as possible. We are not compilers.

    kind regards,

    Jos

  9. #9
    dark_metal's Avatar
    dark_metal is offline Member
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    9
    Rep Power
    0

    Default 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 dereference
    Last edited by dark_metal; 04-05-2010 at 07:00 PM. Reason: type of error not stated

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dark_metal View Post
    char cannot be dereference
    Your compile whines about this line:

    Java Code:
    for(i=0; i<storeLetter[i].length(); i++){
    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.

    kind regards,

    Jos

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. array of char
    By sinisab in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 09:48 AM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. pop from stack and store to char?
    By viligante8 in forum New To Java
    Replies: 13
    Last Post: 11-02-2008, 02:00 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
  •