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

    Thumbs up Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102

    im doing an assignment which requires me to implement a program which auto generate alphabets from A to Z and prompting the user to key in a value.
    The question is like this:

    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.

    I have done the code but got one errors there.I do realize int j cannot be initialized but how can make it so that it can read the array variable letter[]?

    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 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 = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    		//Hold the letter each time it is created
    	private	char letter;
    
    		//Array to hold letter generated
    	private char [] genLetter = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    	private	int count=0;
    	private int score=0;
    	[B]private int j;[/B]
    	
    	public lol(){
    		setSize(220,240);
    		setLocation(300,500);
    		
    		letter   = (char)(Math.random() * 26 + 'a');
    		
    		btnSubmit = new JButton("Submit");
    		btnSubmit.addActionListener(this);
    		
    		btnClear  = new JButton("Clear");
    		btnClear.addActionListener(this);
    		
    		lblQuesNo = new JLabel("Question No");
    		lblPromptQues = new JLabel("What letter comes after ");
    		lblDisplayLetter = new JLabel(Character.toString(genLetter[letter]) + "?");
    		
    		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(new JTextField(5));
    
    		lblCorrectWrong = new JLabel();
    		
    		p3 = new JPanel();
    		p3.add(btnSubmit);
    		p3.add(btnClear);
    
    		c = getContentPane();
    		c.setLayout(new GridLayout(4,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.Please key in your answer:");
    			else if(txtInput.getText().length() >=1 && count<20){
    				count++;	
    					
    
    				[B]while(j<letter+count){
    					if(genLetter[letter]==storeLetter[j])
    						break;
    				j++;		
    				}[/B]
    				if(Character.toUpperCase(txtInput.getText().charAt(0))==storeLetter[j+1]){
    					score++;
    					lblCorrectWrong.setText("Correct!");
    				}else{
    					lblCorrectWrong.setText("Incorrect!");
    				}
    				if(count>21)
    					lblCorrectWrong.setText("Your score is " + Integer.toString(score) + "out of " + "20");
    				
    				int countAlpha=0;
    				while(countAlpha<=25){
    					genLetter[countAlpha] = genLetter[countAlpha+1];
    				}		
    				letter   = (char)(Math.random() * 26 + 'a');
         			lblDisplayLetter.setText(Character.toString(genLetter[letter]) + "?");
    			}
    		}
    }
    	public static void main(String[] args){
    				
    		lol gui = new lol();
    		gui.setVisible(true);
    		gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    }
    Last edited by dark_metal; 04-02-2010 at 11:15 AM. Reason: need to highlight my possible error/problem

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

    Default

    Quote Originally Posted by dark_metal View Post
    Java Code:
    		letter   = (char)(Math.random() * 26 + 'a');
    ...		
    					if(genLetter[letter]==storeLetter[j])
    the value 102 represents the character 'f' and your array doesn't have that many elements hence the ArrayIndexOutOfBounds exception.

    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

    i have change it to ->letter = (char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1));
    as i found out that only this way i can generate alphabets from A - Z...
    One thing im not understand is that I has created array to store 26 of the characters and i have cast the ascii alphabets to characters.one character not occupy one array space?
    So why my array still out of bound?

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

    Default

    Quote Originally Posted by dark_metal View Post
    i have change it to ->letter = (char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1));
    as i found out that only this way i can generate alphabets from A - Z...
    One thing im not understand is that I has created array to store 26 of the characters and i have cast the ascii alphabets to characters.one character not occupy one array space?
    So why my array still out of bound?
    You're thinking way too complicated; here's a spoiler:

    Java Code:
    Random r= new Random();
    char chr= (char)('a'+r.nextInt(26)); // generate letter a ... y
    char nextChr= (char)(chr+1);
    Note that casts to char type.

    kind regards,

    Jos
    Last edited by JosAH; 04-05-2010 at 02:56 PM.

  5. #5
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Smile

    How wonderful the conversation is! Thank JosAH!
    Dear dark_metal,
    there is a true fact you should know when you learn Array of java, that is:
    the first element of an array in java is arrayName[0]! If an array wang[] has 5 elements, and the element wang[5] will out of its bounds, got it?
    In your code:
    Java Code:
    while(countAlpha <=25 ) {
        genLetter[countAlpha] = genLetter[countAlpha + 1];
    ...
    the countAlpha will increase to 25, and the right of '=' will become genLetter[26] here, like I says above, your genLetter[] array has exactly 26 elements, and the last one is genLetter[25]!
    and there is also some else errors like that.
    I have summarized all your code, and found 3 errors, all of them are ArrayIndexOutOfBoundsException, they are:
    1. Line45
    Java Code:
    lblDisplayLetter = new JLabel(Character.toString(genLetter[letter]) + "?");
    2. Line104
    Java Code:
    genLetter[countAlpha] = genLetter[countAlpha+1];
    3. Line107
    Java Code:
    lblDisplayLetter.setText(Character.toString(genLetter[letter]) + "?");
    Good luck!

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

    Default how not to repeat asking the same alphabet?

    i have fixed the errors as you all mentioned.
    right now im wondering how not to repeat asking the same alphabets again.
    i thought of having the alphabets generated store inside storeLetter[]..but how??

    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 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;
    	
    	private	int count=0;
    	private int score=0;
    	private int j;
    	
    	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);
    		
    		//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){		
    		lol gui = new lol();
    		gui.setVisible(true);
    		gui.setDefaultCloseOperation(EXIT_ON_CLOSE);	
    	}
    }

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •