Hi everyone, I am doing a program here to ask question about alphabet, and i am only in the middle of it and i face some problem that i really cant solve, need some help here...

ok the problem is, for(int j =0; j<20;j++),
in output, the 1st jlabel.setText(j) is 0, 2nd straight away became 19, means it skipped from 1 to 18, really want to know whats wrong, here is my code

btw if your are copying this codes into program and trying it, in the output, just ignore everything in the 1st page and press "Begin" button to begin, because things have not been set up properly yet, not until i fix this...

import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class KindergartenTest extends JFrame implements ActionListener{
	
	private JTextField answer;
	private JLabel ans;
	private JTextField result;
	private JLabel Question;
	private JButton next;
	private int[] number = new int[20];
	private char[] alphabet = new char[20];


	
	public KindergartenTest()
	{
		setTitle("Alphabetical Order Test");
		setSize(300,250);
		Container KindergartenTest = getContentPane();
		KindergartenTest.setLayout(new FlowLayout());
		
		setAlphabet(0);
		Question = new JLabel("Alphabet Test\n");
		ans = new JLabel("Name");
		answer = new JTextField(10);
		next = new JButton("Begin");
		restart = new JButton("Restart");
		result = new JTextField(15);
		
		next.addActionListener(this);
		restart.addActionListener(this);
		answer.addActionListener(this);

		KindergartenTest.add(Question);
		KindergartenTest.add(ans);
		KindergartenTest.add(answer);
		KindergartenTest.add(next);
		KindergartenTest.add(result);
		KindergartenTest.add(restart);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public char getAlphabet(int x){
		return alphabet[x];
	}
	
	public void setAlphabet(int z){

		for(int i=z; i<20; i++){
			Random generator = new Random();
			number[i] = 65 + generator.nextInt(25);
			alphabet[i] = (char)number[i];
		}
	}
	
	public void compare(int letter, int j){
		if ( letter == getAlphabet(j)+1 || letter == getAlphabet(j)+33 )
		{
			result.setText("You are Correct");
		}
		else if ( letter != getAlphabet(j)+1 || letter != getAlphabet(j)+33)
		{
			result.setText("You are incorrect");
		}
	}

	public void actionPerformed(ActionEvent event){

		String input1;
		char letter;
		for (int j = 0; j < 20; j++)
		{
			Question.setText("Question " + j+1 + ": What comes after " + getAlphabet(j) + "?");
			ans.setText("Answer");
			next.setText("Next Question! (Question " + j+2 + ")" );
			
			input1 = answer.getText();
		
			letter = input1.charAt(0);
		
			if(event.getSource() == next)
			{
				compare(letter, j);
			}
				
		}
			
	}

	

	public static void main(String[] args){

		KindergartenTest showOutput = new KindergartenTest();
		showOutput.setVisible(true);
	}
	
	
	
}

by the way, if i change the code to

if(event.getSource() == next)
{	
			if (  letter == getAlphabet(0)+1 || letter == getAlphabet(0)+33 )
			{
				result.setText("You are Correct");
			}
			else if ( letter != getAlphabet(0)+1 || letter != getAlphabet(0)+33){
				result.setText("You are incorrect");
			}		
}

i can succesfully compare my input to alphabet[0], but not when i make it into for loop, anyone know whats the problem?
and i guess there is also some problem with my way of putting event.getSource == next, because if i put it to check [0] follows by [1], the comparison for 1st click, [0], will sure be incorrect and it only works well in 2nd click,[1].
please help me, and thank you in advance for everyone that are trying to help me.

Your interface isn't very intuitive. I just ran it to try to help you out but I really have no idea what I'm supposed to be doing and what isn't working. It already said "You are incorrect" before I'd even answered, and it asked me to "name the test" but after I named it, it put the name of the test into the answer column....

Also, I'm having trouble helping you because I don't understand your variables. Why is your alphabet array 20 long? Shouldn't it be 26 long? And what is the setAlphabet method supposed to be doing? It looks like it is supposed to be figuring out the program's next guess (i.e. program asks user "what's after u?") but I can't tell. I'm not sure why you need number[] either. Basically, use some comments and document what is going on, and make clear method names.

sorry for that, im a newbie in java as well as posting codes in forum so forget to add in some description along with the codes, and the program is not finished so i have not rearrange it nicely because im still keep testing something when im doing.

ok, im suppose to create a program to ask user to answer the question "What comes after x" (x is an RandomAlphabet), and the total number of question is 20 so i have my array with size 20.
i am not sure where to create random alphabets and store it into the array alphabet[], so i made a setAlphabet to do so. i am using my generator.nextInt(25) to generate random Integer, and i then convert it into character by adding 65 to it. (ASCII A is 65 and from A to Y is 25, there is no char after Z so i wont ask a question of what comes after Z so its 25 instead of 26. )

So when i press Begin, im using setText to change my JLabel to Question j, as in Question 1 Question 2 Question 3 until 20.
and in the For Loop, when j = 0, i will show Question j+1 : What comes after alphabet[j]? . Eg Question 1: What comes after D?
and i will use answer.getText to get input and compare the 1st character in the string input to my alphabet[j]+1. Means if i typed E, i will compare E to alphabet[j]+1 means D+1 means E.
in the For Loop, If i press the button Next Question!, i will compare them, and setText the JTextField result to inform the user whether it is correct or not. and here is where my problem come.
from here, after printing COrrect or Incorrect, i think it should END IF and END FORLOOP, and restart the FORLOOP with j++, means j+1, and the logically i think it should Question.setText the JLabel to Question 2 since its now j=1. But why after i press Next Question it jump to Question 191 instead of Question 2?

this is the 1st problem and the second is, you try to answer the 1st question, no matter what you type it will be incorrect and try the Question 19 or Question 191, you answer the question and it works well and tells You Are Correct if you are correct. here is another thing that i dont understand why...
thanks for helping anyway!

nevermind i think i can stop it from looping too fast from 1 - 20 instead of going Q2 and wait for me to answer again. checking out what can i do again to fix another problem, so just hold this thread 1st...

here is my new code and new error, the loops never loop at all, j never ++. try to run it and notice the output Question 01: Question 01: Question 01: no matter how many times u tried answering...

import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class KindergartenTest extends JFrame implements ActionListener{
	
	private JTextField answer;
	private JLabel ans;
	private JButton restart;
	private JTextArea result;
	private JLabel Question;
	private JButton next;
	private int count = 0;
	private int[] number = new int[20];
	private char[] alphabet = new char[20];
	private String nameInput;


	
	public KindergartenTest()
	{
		setTitle("Alphabetical Order Test");
		setSize(300,250);
		Container KindergartenTest = getContentPane();
		KindergartenTest.setLayout(new FlowLayout());
		
		setAlphabet(0);
		Question = new JLabel("Alphabet Test\n ||");
		ans = new JLabel("Please Enter Your Name:");
		answer = new JTextField(10);
		next = new JButton("Begin");
		restart = new JButton("Restart");
		result = new JTextArea(5, 10);
		
		next.addActionListener(this);
		restart.addActionListener(this);
		answer.addActionListener(this);

		KindergartenTest.add(Question);
		KindergartenTest.add(ans);
		KindergartenTest.add(answer);
		KindergartenTest.add(next);
		KindergartenTest.add(result);
		KindergartenTest.add(restart);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public char getAlphabet(int x){
		return alphabet[x];
	}
	
	public void setAlphabet(int z){ //get random numbers[] and convert into alphabets[]

		for(int i=z; i<20; i++){
			Random generator = new Random();
			number[i] = 65 + generator.nextInt(25);
			alphabet[i] = (char)number[i];
		}
	}
	

	public void actionPerformed(ActionEvent event){

		String text = (String)event.getActionCommand();
		if(text.equals("Begin"))//ask user to key in name and start test
		{	
		nameInput = answer.getText();
		result.append("Student Name: " + nameInput + "\n");
		answer.setText("");
		Question.setText("Question 1: What comes after " + getAlphabet(0) + "?");
		ans.setText("Answer");
		next.setText("Confirm" );
		}
		String input1;
		char letter;
		

		for (int j=0;j<20;j++)//when start, require user to type in answer, and when confirm button is pressed, compare answer and alphabet[j], change question to next question and end loop, with j++
		{

			input1 = answer.getText();		
			letter = input1.charAt(0);
		
			if(text.equals("Confirm"))
			{
				if ( letter == getAlphabet(j)+1 || letter == getAlphabet(j)+33 )
				{
					result.append("Question " + j+1 + ": You are Correct\n");
				}
				else if ( letter != getAlphabet(j)+1 || letter != getAlphabet(j)+33)
				{
					result.append("Question " + j+1 + ": You are incorrect\n");
				}
				answer.setText("");
				Question.setText("Question " + j+2 + ": What comes after " + getAlphabet(j+1) + "?");
			//	next.setText("Next" );         (if i use while loop)
			}
			
		/*	if(text.equals("Next"))               (if i use while loop)
			{
				next.setText("Confirm");
				j++;
			}
			else
				++j;*/
				
			/*if(event.getSource() == restart)
			{
				this.setAlphabet(0);
				j = 0;
			}*/
		}
			
	}

	

	public static void main(String[] args){

		KindergartenTest showOutput = new KindergartenTest();
		showOutput.setVisible(true);
	}
	
	
	
}

The loop stop after changing question and doesnt go for loop with j=1 j=2 and so on...whats wrong with it...im getting fed up...really duno whats wrong with my codes...please help me...thank you...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.