Hey so basically I have an assignment to make a simple chatter bot, the purpose of he program is to have a user input a string with a JOptionpane
and then the program will search the user input the see if anything they wrote contained a key word I specified, if so they program will display a certain message. So far iv written it using if-else statements but the teacher wants us to use Arrays (which I have no idea how they work and we are just expected to know)
import javax.swing.JOptionPane;
public class ChatterBot {
public static void main(String args[]) {
String input = "";
String maths = "";
String science = "";
String chemFact = "";
String bioFact = "";
String zooFact = "";
String algFact = "";
String yes = "Well good for you";
String no = "You learn something new everyday :)";
input = JOptionPane
.showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");
if (input.contains("science")) {
science = JOptionPane.showInputDialog(
"What kind of science fact woukd you like to know about? (chem, Biology, Zoology)");
}
else if (input.contains("maths")) {
maths = JOptionPane.showInputDialog(
"What kind of maths fact would you like to know about? (algebra, fractions, division) ");
}
if (maths.contains("algebra")) {
algFact = JOptionPane.showInputDialog(
"\"Did you know a mathematician who specializes in algebra is called an algebraist? (yes or no)\"");
}
if (algFact.contains("yes")) {
System.out.println(yes);
} else if (algFact.contains("no")) {
System.out.println(no);
}
if (science.contains("chem")) {
chemFact = JOptionPane.showInputDialog(
"Did you know If you pour a handful of salt into a full glass of water the water level will actually go down rather than overflowing the glass? (yes or no)");
}
if (chemFact.contains("yes")) {
System.out.println(yes);
} else if (chemFact.contains("no")) {
System.out.println(no);
}
else if (science.contains("biology")) {
bioFact = JOptionPane.showInputDialog("Did you know The brain itself cannot feel pain? (yes or no)");
}
if (bioFact.contains("yes")) {
System.out.println("Well good for you");
} else if (bioFact.contains("no")) {
System.out.println("You learn something new everyday :)");
}
else if (science.contains("zoology")) {
zooFact = JOptionPane
.showInputDialog("Did you know butterflies have taste receptors on their feet? (yes or no)");
}
if (zooFact.contains("yes")) {
System.out.println("Well good for you");
} else if (zooFact.contains("no")) {
System.out.println("You learn something new everyday :)");
}
if (input.contains("?")) {
System.out.println("I will be asking the questions");
}
}
There are plenty of good tutorials online about java arrays. Additionally, if your class is following a text book of any sort, then it should also have arrays covered as well. https://www.tutorialspoint.com/java/java_arrays.html
Just from a quick google.
In general an array is a data structure that holds objects in a list like function.
Generically speaking
type[] var = new type[size];
or
type[] var = {foo0, foo1, foo2...};
Real Examples
int[] intergerArray = new int[10];
String[] stringArray = {"Hello", "World"};
Indexes Generically
var = array variable
index = position of object - 1 (computers start at 0)
var[index]
will return the value stored in position index
from the var
array
Index Examples
Make the array:
String[] stringArray = {"This", "is", "my", "first", "array"};
Access the first value:
stringArray[0];
Store the value in a variable:
String firstWord = stringArray[0];
You can even iterate through the entire array all at once:
for (int i = 0; i < stringArray.length; i++){
System.out.print(stringArray[i]);
}
Outputs:
This is my first array
For your code
I'd recommend putting your possible inputs in an array (or even in a couple of arrays)
String[] subjects = {"English", "Science", "Maths"};
You can then accept an input from the user and loop through your array to see if it matches one of your supported inputs. Also, generally you want to include a 'default' case for invalid input.
Possible Implementation
import javax.swing.JOptionPane;
public class ChatterBot{
public static void main(String[] args){
String[] subjects = {"English", "Maths", "Science"};
String userInput = JOptionPane
.showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");
if (userInput.contains(subjects[0]){
// english facts
} else if (userInput.contains(subjects[1]){
// science facts
} else if (userInput.contains(subjects[2])){
// maths facts
}
}
}