Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Strings are not printing as they should?

    Im trying to make a question game, much like a spin off from Trivial Pursuit. In this code, I call classes to get a random number. This number determines what category the question will be from. Coinciding with this number, the "if" statements go and pull the questions and answers from an alternate class. My problem is that when I try and output what should be the question and the 3 answers, its outputs "null" for each String?

    This is my first class, which is just the class for the player.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    import java.util.Scanner;
    /**
     *
     * @author 358512
     */
    public class playerTwo {
        public int playerTwoCounter = 0;
        
        public int playerTwoTurn(){
            methods methods = new methods();
            int rollDice = methods.rollDice();
            return rollDice;
        }
    
        public void playerTwoGame(){
            System.out.println("\nPlayer 2, here is your question!\n");
            playerTurn();
        }
        
        public void playerTurn(){
    //Creates object for the methods class and gets question        
            methods methods = new methods();
            int questionNumber = methods.getQuestion();
            
    //Create variable to store question, answers, and the definate answers
            String question = "";
            String answer1 = "";
            String answer2 = "";
            String answer3 = "";
            int answerDefinate = 0;
            
            
    //Determine what category to get question from, and call the questions and answers; store in created variables
            if(questionNumber == 1){
                naturalWorld getNWQuestion = new naturalWorld();
                question = getNWQuestion.question();
                answer1 = getNWQuestion.answers();
                answer2 = getNWQuestion.answers();
                answer3 = getNWQuestion.answers();
                answerDefinate = getNWQuestion.answerDef();
            }
            if(questionNumber == 2){
                religionEthics getREQuestion = new religionEthics();
                question = getREQuestion.question();
                answer1 = getREQuestion.answers();
                answer2 = getREQuestion.answers();
                answer3 = getREQuestion.answers();
                answerDefinate = getREQuestion.answerDef();                        
            }
            if(questionNumber == 3){
                scienceTechnology getSTQuestion = new scienceTechnology();
                question = getSTQuestion.question();
                answer1 = getSTQuestion.answers();
                answer2 = getSTQuestion.answers();
                answer3 = getSTQuestion.answers();
                answerDefinate = getSTQuestion.answerDef();                       
            }
            if(questionNumber == 4){
                pie getPieQuestion = new pie();
                question = getPieQuestion.question();
                answer1 = getPieQuestion.answers();
                answer2 = getPieQuestion.answers();
                answer3 = getPieQuestion.answers();
                answerDefinate = getPieQuestion.answerDef();                       
            } 
            
    //Call method to display the question        
            displayQuestion(question, answer1, answer2, answer3, answerDefinate);
    
    //Call method to get user input        
            int myAnswer = getInput();
            
    //Call method to check the answer
            checkQuestion(myAnswer, answerDefinate);
        }
    
        public void displayQuestion(String question, String answer1, String answer2, String answer3, int answerDefinate){
            System.out.println(question);
            System.out.println("Type 1: " + answer1 + "\n" + "Type 2: " + answer2 + "\n" + "Type 3: " + answer3);
        }
        
        public int getInput(){
            Scanner myInput = new Scanner(System.in);
            int myAnswer = myInput.nextInt();
            return myAnswer;
        }
        
        public void checkQuestion(int myAnswer, int answerDefinate){
            if(myAnswer == answerDefinate){
                System.out.println("You have got the correct answer! You may go again.\n");
                playerTwoCounter++;
            }
            else{
                System.out.println("You have got the incorrect answer. It is now player 1's turn.\n");
                playerOne turnSwitch = new playerOne();
                turnSwitch.playerOneTurn();
            }
        }
    }
    This is a class for one of the categories (assume that the number chosen by the random number generator is 1).

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    
    /**
     *
     * @author 358512
     */
    public class naturalWorld {
        public String questionArray[] = new String[10];
        public String answerArray[] = new String[30];
        public int answerDef[] = new int[10];
        public String question;
        public String answer;
        public int defAnswer;
        public int d = 0;
        public int a = 0;
        public int s = 0;
    
        public void questionSet(){
    //This method will insert the question into each category
            questionArray[0] = "Large deposits of silt, till, sands, and gravels are known as what?";
            questionArray[1] = "The force that drives the movement of the continents is: ";
            questionArray[2] = "This scale is used to measure the intensity of earthquakes.";
            questionArray[3] = "A person who studies climates is known as a: ";
            questionArray[4] = "An imaginary line at zero degrees longitude: ";
            questionArray[5] = "In Earth's geologic history, there has been __ eras.";
            questionArray[6] = "In terms of the study of global warming, GCM stands for: ";
            questionArray[7] = "An apogy is: ";
            questionArray[8] = "The first bacteria to introduce oxygen into the Earth's atmosphere through the process of photosynthesis are: ";
            questionArray[9] = "The strongest of all rock types are: ";
        }
    
        public void answerSet(){
    //This method will insert 3 answers for each question
            answerArray[0] = "Moraines";
            answerArray[1] = "Mountains";
            answerArray[2] = "Bedrock";
            answerArray[3] = "Wind";
            answerArray[4] = "Ocean Currents";
            answerArray[5] = "Convection Currents";
            answerArray[6] = "Colombian Scale";
            answerArray[7] = "Richter Scale";
            answerArray[8] = "Quake Scale";
            answerArray[9] = "Geologist";
            answerArray[10] = "Climatologist";
            answerArray[11] = "Meteorologist";
            answerArray[12] = "Prime Meridian";
            answerArray[13] = "Equator";
            answerArray[14] = "The International Date Line";
            answerArray[15] = "4";
            answerArray[16] = "2";
            answerArray[17] = "3";
            answerArray[18] = "General Circulation Model";
            answerArray[19] = "Global Climate Model";
            answerArray[20] = "Global Circulation Model";
            answerArray[21] = "When the Moon is at the closest point in its orbit around the Earth.";
            answerArray[22] = "When the Earth is at the farthest point in its orbit around the sun.";
            answerArray[23] = "When the Solar System is at the height of its natural expansion.";
            answerArray[24] = "Bacillus Subtilis";
            answerArray[25] = "Mycobacterium Tuberculosis";
            answerArray[26] = "Cyano Bacteria";
            answerArray[27] = "Igneous";
            answerArray[28] = "Sedimentary";
            answerArray[29] = "Metamorphic";
        }
    
        public void answerDefSet(){
            answerDef[0] = 1;
            answerDef[1] = 3;
            answerDef[2] = 2;
            answerDef[3] = 2;
            answerDef[4] = 1;
            answerDef[5] = 1;
            answerDef[6] = 3;
            answerDef[7] = 2;
            answerDef[8] = 3;
            answerDef[9] = 1;
        }
    
        public String question(){
    //This method gets a question from the array and sends it back to the method to be asked to the user
            question = questionArray[d];
            d++;
            return question;
        }
    
        public String answers(){
            answer = answerArray[a];
            a++;
            return answer;
        }
    
        public int answerDef(){
            defAnswer = answerDef[s];
            s++;
            return defAnswer;        
        }
    }
    Finally, this is a small methods class. At the bottom, there is a method called getQuestion. This generate the random number.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    import javax.swing.*;
    
    public class methods {
        public int playerOneDice, playerTwoDice;
        public void playGame(){
            int playGUI = JOptionPane.showConfirmDialog(null, "Are you ready to play Trivial Pursuit: School Edition?", "Trivial Pursuit: School Edition", JOptionPane.YES_NO_OPTION);
    
    //Determine the button clicked by the user
            if(playGUI == 1){
                System.exit(0);
            }
            else{
            }
    
    //Call method and class to set all the questions into their respective arrays
            scienceTechnology scienceTechnology = new scienceTechnology();
            scienceTechnology.questionSet();
            scienceTechnology.answerSet();
            scienceTechnology.answerDefSet();
    
            religionEthics religionEthics = new religionEthics();
            religionEthics.questionSet();
            religionEthics.answerSet();
            religionEthics.answerDefSet();
    
            naturalWorld naturalWorld = new naturalWorld();
            naturalWorld.questionSet();
            naturalWorld.answerSet();
            naturalWorld.answerDefSet();
    
    //Intro information
            System.out.println("Welcome to Trivial Pursuit: School Edition!"
                    + "\n\nPlayer 1: Lets roll the dice to decide who goes first!");
    
    //Call method to determine what player goes first
            playerFirstDice();
    
    //Determine and output who goes first
            if(playerOneDice > playerTwoDice){
                System.out.println("Player 1 rolled a " + playerOneDice + ".\nPlayer 2 rolled a " + playerTwoDice + "."
                        + "\nPlayer 1 goes first!");
                playerOne playerOne = new playerOne();
                playerOne.playerOneGame();
            }
            if(playerTwoDice > playerOneDice){
                System.out.println("Player 1 rolled a " + playerOneDice + ".\nPlayer 2 rolled a " + playerTwoDice + "."
                        + "\nPlayer 2 goes first!");
                playerTwo playerTwo = new playerTwo();
                playerTwo.playerTwoGame();
            }
            if(playerOneDice == playerTwoDice){
                System.out.println("Player 1 rolled a " + playerOneDice + ".\nPlayer 2 rolled a " + playerTwoDice + "."
                        + "\nPlease try again.");
                System.exit(0);
            }
        }
    
        public void playerFirstDice(){
    //Player 1 rolls dice to determine who goes first
            playerOne playerOneTurn = new playerOne();
            playerOneDice = playerOneTurn.playerOneTurn();
    
    //Player 2 rolls dice to determine who goes first
            playerTwo playerTwoTurn = new playerTwo();
            playerTwoDice = playerTwoTurn.playerTwoTurn();         
        }
    
        public int rollDice(){
    //Creates random number to serve as the dice roll
            int diceRoll = (int)((Math.random() * 6) + 1);
            return diceRoll;
        }
    
        public int getQuestion(){
    //Creates random number to serve as finding out what question to get and from what category
            int questionNumber = (int)((Math.random() * 3) + 1);
            return questionNumber;
        }
    
    }
    This is it. Its quite frustrating. Any ideas?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Can you copy the full contents of the command prompt window from when you execute the code so we can see what it does?

    outputs "null" for each String?
    What variables have the values you are trying to print? Where are the statements that are printing the contents of those variables?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    Quote Originally Posted by Norm View Post
    Can you copy the full contents of the command prompt window from when you execute the code so we can see what it does?


    What variables have the values you are trying to print? Where are the statements that are printing the contents of those variables?
    This is just the opening class. All it basically does it run the game.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    
    /**
     * Name: Ben Hughes
     * Date: Thursday May 29th, 2014
     * Teacher's Name: Mr. Montgomery
     * Course Code: ICS3U
     * Program Description: Play a game of trivial pursuit!
     */
    public class Main {
        public static void main(String[] args){
    
    //Call class methods to determine if user wants to play the game
        methods methods = new methods();
        methods.playGame();
        }
    }

  4. #4
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    question, answer1, answer2, and answer3 have the values I want to print (They are found in the playerTwo class). However, when I attempt to print them, they come up as null.

    Oh, and the statements trying to print the value are in the class PlayerTwo and the method displayQuestion.

    To my knowledge, when I call the category class and the pertinent methods, the code should set the question variable in that class to whatever the string is at the desired position in the array, then return the value.
    Last edited by madtaffy; 06-13-2014 at 03:32 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Can you post the contents of the command prompt window from when the program is executed that shows the problem?

    Where is the statement(s) that print the contents of the variables? Please post the class name and the line number.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    This is what it says:

    Welcome to Trivial Pursuit: School Edition!

    Player 1: Lets roll the dice to decide who goes first!
    Player 1 rolled a 2.
    Player 2 rolled a 1.
    Player 1 goes first!

    Player 1, here is your question!
    null
    Type 1: null
    Type 2: null
    Type 3: null

    And the System.out.println() statements are in the class playerTwo and on lines 84 and 85.
    Last edited by madtaffy; 06-13-2014 at 03:39 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Where are the values for the variables set? Backtrack in the code to see why the values of the variables are null. What is returned by the methods that set their values? Do the methods return the null values?

    Try debugging by adding some println() statements that show what values are chosen and returned by the methods.

    Player 1, here is your question!
    Where is that line printed? I don't see it in the posted code.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    I set these variables to blank values in line 32-35 in the playerTwo class. However, I set them to the Strings within the if statements in the playerTwo class (I call the classes and the category class/method should return the string and set it to the variable). I placed println() statements in the question() method in the naturalWorld class... its prints out null even inside that class... D:

    And that player 1, here is your question thing... its the same for player 2. It would be printed in line 22 in the playerTwo class, however, when i ran the program to get that command prompt, Player 1 got to go first :p
    Last edited by madtaffy; 06-13-2014 at 04:11 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    its prints out null even inside that class.
    Where is a value given to the elements in the array? Is that method being called for the instance of the methods class that the code is using?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    In the methods class, I call the methods in the category class to set the values in the array (line 23)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    If the method is called, why are the values in the arrays null?

    Add a println() statement to all of the constructors of the classes and the methods that set the contents of the arrays so you can see what the code is doing when it executes.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    I must leave now :( but ill ensure that I do so for tomorrow, and I shall reply with my response.

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Strings are not printing as they should?

    Hi,

    The problem seems to be your class that holds all questions / answers:
    I do not see a constructor. So java creates a new default constructor for you which is then used by you.
    That constructor only initializes all your elements, so your arrays are initialized as empty arrays.
    Your methods that initialize the array are never called! So you might want to add an constructor that then calls your initialisation code.

    After fixing that issue you might run into the next issue: You always create a new oject for our questions. So your pointers will be reset each time and you will always get the first question. So you might want to declare it just once....

    Are you interested in object oriented design? Should I bother you with some optimisations so you get rid of double code? Or will that confuse you only? If you are interested, then we might want to do a few changes on your code together so you see an example how you could build cleaner code.

    With kind regards,

    Konrad

  14. #14
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default Re: Strings are not printing as they should?

    please check if ur code is gng in question number if condition.put some printstatements inside each if.

    If possible post the whole code..ur code is incomplete.

    if(questionNumber == 1){
    naturalWorld getNWQuestion = new naturalWorld();
    question = getNWQuestion.question();
    answer1 = getNWQuestion.answers();
    answer2 = getNWQuestion.answers();
    answer3 = getNWQuestion.answers();
    answerDefinate = getNWQuestion.answerDef();
    }
    Ramya:cool:

  15. #15
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    What initialization code are you referring to?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Did you try debugging the code by adding println() statements so you can see when the different parts of the code are executing?

    The other posters are finding the bugs for you instead of letting you find them. It's shorter to let others find your problems, but you need to be able to find them yourself.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Strings are not printing as they should?

    Check your class naturalWorld. You have methods that initialize your arrway e.g. questionSet.
    It seems that these methods are never called.

    That was what I meant.

  18. #18
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    Quote Originally Posted by Norm View Post
    Did you try debugging the code by adding println() statements so you can see when the different parts of the code are executing?

    The other posters are finding the bugs for you instead of letting you find them. It's shorter to let others find your problems, but you need to be able to find them yourself.
    I did, but I don't think I understood constructers enough to be able to realize that that was my problem... but I've run into another problem... and I've spent 3 days inserting println() statements and debugging the code and I just cannot figure it out... would you care to give me a hand?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Post the new version of the code.
    Also execute the code, copy what is printed out and paste it here.
    There should be clues in the print out about what the code is doing. It might be necessary to add more to what is printed and to have more println()s to show what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    So this is my naturalWorld class now... I put the questions into a constructor and it now prints out the questions, answers, and the definite answers :)

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    
    /**
     *
     * @author 358512
     */
    public class naturalWorld {
        public String questionArray[] = new String[10];
        public String answerArray[] = new String[30];
        public int answerDef[] = new int[10];
        public String question;
        public String answer;
        public int defAnswer;
        public int z = 0;
        public int x = 0;
        public int c = 0;
    
        public naturalWorld(){
    //This method will insert the question into each category
            questionArray[0] = "Large deposits of silt, till, sands, and gravels are known as what?";
            questionArray[1] = "The force that drives the movement of the continents is: ";
            questionArray[2] = "This scale is used to measure the intensity of earthquakes.";
            questionArray[3] = "A person who studies climates is known as a: ";
            questionArray[4] = "An imaginary line at zero degrees longitude: ";
            questionArray[5] = "In Earth's geologic history, there has been __ eras.";
            questionArray[6] = "In terms of the study of global warming, GCM stands for: ";
            questionArray[7] = "An apogy is: ";
            questionArray[8] = "The first bacteria to introduce oxygen into the Earth's atmosphere through the process of photosynthesis are: ";
            questionArray[9] = "The strongest of all rock types are: ";
    
    //This method will insert 3 answers for each question
            answerArray[0] = "Moraines";
            answerArray[1] = "Mountains";
            answerArray[2] = "Bedrock";
            answerArray[3] = "Wind";
            answerArray[4] = "Ocean Currents";
            answerArray[5] = "Convection Currents";
            answerArray[6] = "Colombian Scale";
            answerArray[7] = "Richter Scale";
            answerArray[8] = "Quake Scale";
            answerArray[9] = "Geologist";
            answerArray[10] = "Climatologist";
            answerArray[11] = "Meteorologist";
            answerArray[12] = "Prime Meridian";
            answerArray[13] = "Equator";
            answerArray[14] = "The International Date Line";
            answerArray[15] = "4";
            answerArray[16] = "2";
            answerArray[17] = "3";
            answerArray[18] = "General Circulation Model";
            answerArray[19] = "Global Climate Model";
            answerArray[20] = "Global Circulation Model";
            answerArray[21] = "When the Moon is at the closest point in its orbit around the Earth.";
            answerArray[22] = "When the Earth is at the farthest point in its orbit around the sun.";
            answerArray[23] = "When the Solar System is at the height of its natural expansion.";
            answerArray[24] = "Bacillus Subtilis";
            answerArray[25] = "Mycobacterium Tuberculosis";
            answerArray[26] = "Cyano Bacteria";
            answerArray[27] = "Igneous";
            answerArray[28] = "Sedimentary";
            answerArray[29] = "Metamorphic";
    
    //Set definate answers
            answerDef[0] = 1;
            answerDef[1] = 3;
            answerDef[2] = 2;
            answerDef[3] = 2;
            answerDef[4] = 1;
            answerDef[5] = 1;
            answerDef[6] = 3;
            answerDef[7] = 2;
            answerDef[8] = 3;
            answerDef[9] = 1;
        }
    
        public String question(){
    //This method gets a question from the array and sends it back to the method to be asked to the user
            question = questionArray[z];
            z++;
            return question;
        }
    
        public String answers(){
            answer = answerArray[x];
            x++;
            return answer;
        }
    
        public int answerDef(){
            defAnswer = answerDef[c];
            c++;
            return defAnswer;        
        }
    }
    There is just a small change in my player class, just so that if someone gets the question right, they can go again.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package trivialpursuit;
    import java.util.Scanner;
    /**
     *
     * @author 358512
     */
    public class playerOne {
        public int playerOneCounter = 0;
        
        public int playerOneTurn(){
            methods methods = new methods();
            int rollDice = methods.rollDice();
            return rollDice;
        }
    
        public void playerOneGame(){
            System.out.println("\nPlayer 1, here is your question!");
            playerTurn();
        }
        
        public void playerTurn(){
    //Creates object for the methods class and gets question        
            methods callMethods = new methods();
            int questionNumber = callMethods.getQuestion();
            
    //Create variable to store question, answers, and the definate answers
            String question = "";
            String answer1 = "";
            String answer2 = "";
            String answer3 = "";
            int answerDefinate = 0;
    
    //Create objects for each category
            naturalWorld getNWQuestion = new naturalWorld();
            religionEthics getREQuestion = new religionEthics();
            scienceTechnology getSTQuestion = new scienceTechnology();
    
    //Determine what category to get question from, and call the questions and answers; store in created variables
            if(questionNumber == 1){
                question = getNWQuestion.question();
                answer1 = getNWQuestion.answers();
                answer2 = getNWQuestion.answers();
                answer3 = getNWQuestion.answers();
                answerDefinate = getNWQuestion.answerDef();
            }
            if(questionNumber == 2){
                question = getREQuestion.question();
                answer1 = getREQuestion.answers();
                answer2 = getREQuestion.answers();
                answer3 = getREQuestion.answers();
                answerDefinate = getREQuestion.answerDef();                        
            }
            if(questionNumber == 3){
                question = getSTQuestion.question();
                answer1 = getSTQuestion.answers();
                answer2 = getSTQuestion.answers();
                answer3 = getSTQuestion.answers();
                answerDefinate = getSTQuestion.answerDef();                       
            }
            
    //Call method to display the question        
            displayQuestion(question, answer1, answer2, answer3, answerDefinate);
    
    //Call method to get user input        
            int myAnswer = getInput();
            
    //Call method to check the answer
            checkQuestion(myAnswer, answerDefinate);
        }
    
        public void displayQuestion(String question, String answer1, String answer2, String answer3, int answerDefinate){
            System.out.println(question);
            System.out.println("Type 1: " + answer1 + "\n" + "Type 2: " + answer2 + "\n" + "Type 3: " + answer3);
        }
        
        public int getInput(){
            Scanner myInput = new Scanner(System.in);
            int myAnswer = myInput.nextInt();
            return myAnswer;
        }
        
        public void checkQuestion(int myAnswer, int answerDefinate){
            if(myAnswer == answerDefinate){
                System.out.println("You have got the correct answer! You may go again.\n");
                playerOneCounter++;
                playerOneGame();
            }
            else{
                System.out.println("You have got the incorrect answer. It is now player 2's turn.\n");
                playerTwo turnSwitch = new playerTwo();
                turnSwitch.playerTwoGame();
            }
        }
    }
    These are the only two things that have been edited. The rest is the same. Now, my problem is, that when I call one of the category class and receive a question (for example), It is supposed to add one to a counter so that the next time I enter that method, the next question in the array will be chosen. It is supposed to do this for the answers and the answerDef as well. However, I can never get off the first question. To what I have done trying to figure it out, all the counters get set back to zero and I don't understand why.

    This is what will print out (keep in mind, the order of the questions will be random, so it won't print out like this all the time. This is just an example to show you that it continues to run the same question over again.):

    Player 1: Lets roll the dice to decide who goes first!
    Player 1 rolled a 1.
    Player 2 rolled a 4.
    Player 2 goes first!

    Player 2, here is your question!

    Large deposits of silt, till, sands, and gravels are known as what?
    Type 1: Moraines
    Type 2: Mountains
    Type 3: Bedrock
    1
    You have got the correct answer! You may go again.


    Player 2, here is your question!

    Large deposits of silt, till, sands, and gravels are known as what?
    Type 1: Moraines
    Type 2: Mountains
    Type 3: Bedrock

    The program never actually launches the next question and set of answers. I can continuously get this one question right.

Page 1 of 2 12 LastLast

Similar Threads

  1. Printing formatted strings to laser printer
    By alan2 in forum New To Java
    Replies: 31
    Last Post: 04-05-2013, 09:40 AM
  2. Printing 5 Strings per line
    By mafro in forum New To Java
    Replies: 14
    Last Post: 05-04-2012, 10:03 AM
  3. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  4. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  5. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 05:45 AM

Posting Permissions

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