Final Project - Code: V.1
- Yavneeka Patel
- Mar 9, 2015
- 3 min read
This was the first draft of the programming. The goals was to have some output given a certain input. This actually took longer than I expected to code, but I'm pretty proud of this simple version.
import java.util.Scanner;
/**
* This class holds the first prototype for the chatterbot
* Goals:
*Have computer respond to certain questions based on user input question
*Print this response to the console
* Problems with this version:
*Using the next() with the Scanner only grabbed the next word and not the entire line
*So none of the arrays were being read
*Solution: nextLine() reads the entire line (thanks Stack Overflow! http://stackoverflow.com/questions/4058912/scanner-doesnt-read-whole-sentence)
*
* References:
*Minesweeper (CS 101)
*Chatterbot in C++ Tutorial: http://www.instructables.com/id/A-Learning-Chatterbot-in-C/
*
* @author patel22y
*/
public class Chatterbot1 {
/***************************** CLASS PROPERTIES **************************/
//holds the value the user inputs
private static Scanner userInput;
//holds the question, response choices
public static String[][] responseDataBase = {
{//if asked "Hello":
"Hola.",
//respond with:
"Hola."
},
{//if asked "who are you?":
"¿Quién eres?",
//respond with:
"Me llama Catalina de España. Soy un robot."
},
{//if asked "where is the bathroom?":
"¿Dónde está el baño?",
//respond with:
"El baño está en la biblioteca"
},
{//if asked "what do you like?":
"¿Que te gustan?",
//respond with:
"Me gusta los perritos y las piña coladas"
}
};
/***************************** INSTANCE METHODS **********************/
/**
* Blank constructor
*/
public Chatterbot1() {
//does nothing
}
/***************************** CLASS METHODS **********************/
/**
* The function runs through the responseDataBase array to determine what question was asked
* And returns the corresponding response
* @param String question
* @return String response
*/
static String findInArray(String input) {
//create a variable called response that will hold the computer response
//initialize the response to a blank string
String response = "";
//for loop will run through the responseDataBase array
for(int i = 0; i < responseDataBase.length; ++i) {
//if the first element of each of the arrays (the question portion)
//matches the user inputed question
if(responseDataBase[i][0].equals(input)) {
//add the second element of that array to the response String
response = responseDataBase[i][1];
}
}
//return the response outside of the for loop
return response;
}
/**
* The main function
* Must throw exception because I am taking in user input
* @param args
*/
public static void main(String[] args)throws Exception {
//create a boolean called isRunning that is initialized to true
//i want to use this boolean to have my loop continue as long as the user does not type "QUIT"
boolean isRunning = true;
//while isRunning = true
while(isRunning) {
//start by printing out the instruction line, and the >>> to indicate the user response goes there
System.out.print("---------------" + "\n" + "Escriba algo | Write something:" + "\n" + ">>>");
//Creates the userInput which is a new Scanner
userInput = new Scanner (System.in);
//Sets the input String equal to the user input
String input = userInput.nextLine();
System.out.println ("You typed: " + input);
//set the response String equal to the value of whatever findInArray returns
String response = findInArray(input);
//if the user input says "I Quit" (Renuncio in Spanish)
if(input.equals("Renuncio")){
//Have the computer acknowledge that they are leaving
System.out.println("¡Hasta luego!");
//stop the while loop by setting isRunning to false
isRunning = false;
}
//else if
//the findInArray was unable to find the question, response String would equal 0
else if(response.length() == 0) {
//print out to user "I don't understand"
System.out.println("No intiendo.");
}
//otherwise
else {
//print out the response
System.out.println(response);
}
}
}
}
Recent Posts
See AllSo we've made it to the very end. Through all the ups and downs and we're all still standing. It was an amazing semester and I made some...
/** *Java Speech Grammar Format (JSGF) file *This file holds all the words that the program will recognize **/...
/* * Copyright 2013 Carnegie Mellon University. * Portions Copyright 2004 Sun Microsystems, Inc. * Portions Copyright 2004 Mitsubishi...
Comentarios