Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Issue With Methods Between Classes

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issue With Methods Between Classes

    Hello! I'm trying to solve this issue, my code is below

    // CLASS ONE: METHODS ONLY
    package project2;
    import java.lang.Math;

    public class PromptBank {

    String [] question;
    String [] statement;

    public PromptBank(){
    question = new String[3]; //initialize your array to the correct length to match your number of questions you populate it with
    statement = new String[3]; //initialize your array to the correct length to match your number of questions you populate it with

    }

    public void populateStatementsArray(){
    statement[0] = "Tell me more about BLANK1 and BLANK2";
    statement[1] = "BLANK1 seems important to you, so does BLANK2. Please tell me more.";
    statement[2] = "BLANK1 and BLANK2 seem to be on your mind. Let's talk about it.";
    }

    public void populateQuestionsArray(){
    question[0] = "Is there anything else about BLANK1 and BLANK2?";
    question[1] = "Does BLANK1 bother you? How about BLANK2?";
    question[2] = "Are BLANK1 and BLANK2 things you think about often?";
    }

    public String getRandomStatement(){ // the goal here is to print a random statement from the statement array when this method is called
    double range = 2 - 0 + 1; //max - min + 1
    double random = (Math.random() * range) + 0;
    String answer = statement[(int)random];

    return answer;
    }
    }

    //CLASS TWO: MAIN METHOD
    package project2;
    import java.util.Scanner;

    public class Eliza {
    public static void main(String [] args) {
    String name = "";
    String input = " ";
    String output = " ";

    // Greetings and asking for name
    System.out.println("Hello, my name is Eliza. What is your name?");

    Scanner scnr = new Scanner(System.in);
    name = scnr.nextLine();

    //What's on your mind?
    System.out.println("Hello, " + name + ". Tell me what is on your mind today in 1 sentence.");
    input = scnr.nextLine();

    // ***** PROBLEM!! - For some reason when I get here, the console just prints "null" I tried taking out the random function from the method thinking it
    //was wrong but it still kept returning null.
    PromptBank answerobj = new PromptBank();
    output = answerobj.getRandomStatement();
    System.out.println(output);

    }

    }

    Thanks so much in advance!

  2. #2
    Junior Member
    Join Date
    Apr 2019
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Issue With Methods Between Classes

    need call answerobj.populateStatementsArray(); first before output = answerobj.getRandomStatement();

    // ***** PROBLEM!! - For some reason when I get here, the console just prints "null" I tried taking out the random function from the method thinking it 
            //was wrong but it still kept returning null. 
            PromptBank answerobj = new PromptBank();
     
            answerobj.populateStatementsArray();
     
            output = answerobj.getRandomStatement();
            System.out.println(output);

    whole code

    package com.tool.util;
     
    import java.lang.Math;
     
    public class PromptBank {
     
        String[] question;
        String[] statement;
     
        public PromptBank() {
            question = new String[3]; //initialize your array to the correct length to match your number of questions you populate it with
            statement = new String[3]; //initialize your array to the correct length to match your number of questions you populate it with
     
        }
     
        public void populateStatementsArray() {
            statement[0] = "Tell me more about BLANK1 and BLANK2";
            statement[1] = "BLANK1 seems important to you, so does BLANK2. Please tell me more.";
            statement[2] = "BLANK1 and BLANK2 seem to be on your mind. Let's talk about it.";
        }
     
        public void populateQuestionsArray() {
            question[0] = "Is there anything else about BLANK1 and BLANK2?";
            question[1] = "Does BLANK1 bother you? How about BLANK2?";
            question[2] = "Are BLANK1 and BLANK2 things you think about often?";
        }
     
        public String getRandomStatement() { // the goal here is to print a random statement from the statement array when this method is called
            double range = 2 - 0 + 1; //max - min + 1
            double random = (Math.random() * range) + 0;
            String answer = statement[(int) random];
     
            return answer;
        }
    }

    package com.tool.util;
     
    import java.util.Scanner;
     
    public class Eliza {
        public static void main(String[] args) {
            String name = "";
            String input = " ";
            String output = " ";
     
            // Greetings and asking for name
            System.out.println("Hello, my name is Eliza. What is your name?");
     
            Scanner scnr = new Scanner(System.in);
            name = scnr.nextLine();
     
            //What's on your mind?
            System.out.println("Hello, " + name + ". Tell me what is on your mind today in 1 sentence.");
            input = scnr.nextLine();
     
            // ***** PROBLEM!! - For some reason when I get here, the console just prints "null" I tried taking out the random function from the method thinking it 
            //was wrong but it still kept returning null. 
            PromptBank answerobj = new PromptBank();
     
            answerobj.populateStatementsArray();
     
            output = answerobj.getRandomStatement();
            System.out.println(output);
     
        }
     
    }

Similar Threads

  1. Exchanging between classes and methods
    By Elyril in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 15th, 2014, 06:00 AM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. Methods In Different Classes
    By Khadafi in forum Java Theory & Questions
    Replies: 2
    Last Post: January 11th, 2012, 06:38 PM
  4. classes and methods??
    By paddy1 in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:02 AM
  5. [SOLVED] Help with Classes/ Instance Methods
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 06:33 PM

Tags for this Thread