HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

"Ella" Java chatbot

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ellachatbotjava

Problem

I recently developed an interest in AI and thought I would develop this little chat bot. Are there any recommendations/modifications?

```
import java.io.*;
import java.util.*;
import java.lang.*;

public class Conversate{

private static String name;

public Conversate(String name){
this.name=name;
}

public static int randomWithRange(int min, int max){
int range = Math.abs(max - min) + 1;
return (int)(Math.random() * range) + (min 15){ System.out.println("Your name is kinda long isn't it!"); }

System.out.println("and your last name is?");
Scanner in2 = new Scanner(System.in);
String namer2 = in2.nextLine();

namer2.trim();
System.out.println("are you a boy or a girl?");
Scanner in3 = new Scanner(System.in);
String gender = in3.nextLine();
char gen = 'f';
if (gender.equalsIgnoreCase("boy") || gender.equalsIgnoreCase("man") || gender.equalsIgnoreCase("guy") || gender.equalsIgnoreCase("m") || gender.equalsIgnoreCase("male") ){
gen = 'm';
System.out.println("oooo...Macho Macho!, Look at you!");
System.out.println("ELLA BLOWS YOU A KISS!");
System.out.println("lol!! I'm a girl.... I guess you can tell.");
}else if (gender.equalsIgnoreCase("girl") || gender.equalsIgnoreCase("woman") || gender.equalsIgnoreCase("lady") || gender.equalsIgnoreCase("f") || gender.equalsIgnoreCase("female") ){
gen = 'f';
System.out.println("OH YEAH!!! GIRL POWER!!!");
}else {
System.out.println("Your answer is not quite clear...");
System.out.println("I'll decide what you are");
try{
Thread.sleep(3000);
}catch (InterruptedException ex){
Thread.currentThread().interrupt();
}
System.out.println("You are a Human BeanStalk! ha ha ha ha!");

Solution

I just have a few general things to point out for now, which should be a good starter:

-
Although you have some additional methods, I still feel that there can be more. It's good to reduce the amount of work done by main(), especially for readability and maintenance concerns. It may also be necessary to use additional classes (the outermost class doesn't have to be the only one).

Regarding the use of classes, you should consider this first. It'll especially be harder to expand on this application without additional classes splitting the various responsibilities. Otherwise, it looks very procedural and harder to follow. Be sure to read up on good Java practices to help bring this application to its fullest potential.

-
This may be a more idiomatic way of acquiring a random number within a range:

// this is constructed in main()
Random rand = new Random();

// ...

public static int randomWithRange(int min, int max, Random rand) {
    return rand.nextInt((max - min) + 1) + min;
}


(The needed import is java.util.Random.)

More alternatives can also be found here.

-
You don't really need System.exit(0) here. There are no loops in this part, so none of that code will be repeated. The program will still gracefully exit from main().

Code Snippets

// this is constructed in main()
Random rand = new Random();

// ...

public static int randomWithRange(int min, int max, Random rand) {
    return rand.nextInt((max - min) + 1) + min;
}

Context

StackExchange Code Review Q#71008, answer score: 8

Revisions (0)

No revisions yet.