Question

Hey guys i am making a program that helps me study.(a flash card machine) It basically has a multi dimensional string array that holds the question and its answer. The first challenge i over came was finding a way to randomly pick a question. What i did is, i made another integer array where i shuffled it on start. what i need to do now is used the integer array to display my multi dimensional arrays content. for example. Lets say my int array started like this {1,2,3,4}. After shuffling it, it changed to {3,1,2,4}. Now i want to use that integer array like this. ArrayOfQuestionsAndAnswers[IntegerArray[0]][0] to get the question. What i dont know how to do is get one question at a time. Everytime a button is clicked the Integer array should make to its next int.(that would be 1 in my example.) How can i do this?

My code so far:

Main class:

public class Core {

    public static void main(String[] args){
        if(Variables.getStart()){
            shuffleArray(Variables.getCardNumber());
            Variables.setStart(false);
        }
    }
   public static String getCardQuestion(){
       return Variables.getCards()[0][0];
   }
    public static String getCardAnswer(){
        return Variables.getCards()[0][1];
    }
    // Implementing Fisher–Yates shuffle
    static void shuffleArray(int[] ar)
    {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--)
        {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }

}

Variable Class:

 public class Variables {
        private static int[] cardNumber ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        private static String[][] cards = {{"Apostolic Orgin", "Comes form the apostles"},
                {"Biblical inerrancy", "the doctrine that the books are free from error reading the truth"},
                {"Divine Inspiration", "the assistance the holy spirit gave the authors or the bible so they could write"},
                {"fundamentalist approach", "interpretation of the bible and christian doctrine based on the literal meaning og bible's word"}, {"pentateuch", "first 5 books of old testament"},
                {"Torah","Means law, refers to first 5 books of the old testament"},{"Sacred Scripture","The bible/approved list of Judism and Christianity"},
                {"Apostolic Succession","passing on of apostolic preaching and authority from apostles to all bishops"},
                {"encumenical council","gathering of bishops form around the world to address issues of the church"},
                {"Breviary","prayer book that contains the prayers for litergy of the hours"}};
        private static boolean start=true;
        private static int index;

        public static void setIndex(int i){
            index=i;
        }
        public static int getIndex(){
            return index;
        }
        public static void setCardNumber(int[] i){
            cardNumber=i;
        }
        public static int[] getCardNumber(){
            return cardNumber;
        }
        public static void setCards(String[][] i){
            cards=i;
        }
        public static String[][] getCards(){
            return cards;
        }
        public static void setStart(boolean i){
             start=i;
        }
        public static boolean getStart(){
            return start;

        }
    }

Basic(un-finished) GUI classs:

import study.religion.firstfinal.core.Core;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI frame = new GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUI() {
        setTitle("Bautista's Religion Review");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JLabel label = new JLabel("");
        label.setBounds(32, 22, 356, 160);
        contentPane.add(label);

        JButton btnShowQuestion = new JButton("Show Question");
        btnShowQuestion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             label.setText("Question: "+Core.getCardQuestion());
            }
        });
        btnShowQuestion.setBounds(62, 216, 121, 23);
        contentPane.add(btnShowQuestion);

        JButton btnShowAnswer = new JButton("Show Answer");
        btnShowAnswer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             label.setText("Answer: "+Core.getCardAnswer());
            }
        });
        btnShowAnswer.setBounds(244, 216, 121, 23);
        contentPane.add(btnShowAnswer);
    }

}
Was it helpful?

Solution

Can you not just create a method called nextQuestion() in your Core class which increments a counter within the class?

Add a integer to your Core class as such:

public class Core {

    private int counter = 0;

Then add a method in your Core class as such:

public void nextQuestion(){
    counter++;
}

Modify your getCardQuestion() and getCardAnswer() to:

public static String getCardQuestion(){
    return Variables.getCards()[counter][0];
}

public static String getCardAnswer(){
    return Variables.getCards()[counter][1];
}

Perhaps add a button that proceeds to the next question and add a ActionListener which calls nextQuestion().

Please note that the nextQuestion() method will keep incrementing without checking for the array boundaries. If you want nextQuestion() to roll over and start again change the statement to such:

counter = (counter + 1) % Variables.getCards().length;

OTHER TIPS

In your GUI class do the following decleration

private int[] RandomInteger = {1,2,3,4}
private int i,j

Then as you say, shuffle it.

Now in nextbutton actionlistener you can do something like that

 if(i < RandomInteger.length())
{
  ArrayofQuesiontAndAnswer(randominteger[i++])[j++] //declare i and j as a class member 
}

Hope it will help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top