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);
}
}