Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning Java myself and I got a "working" code from a Youtube video:

<pre lang="Java">import java.util.Scanner;

public class chatbot {
    
    public static void main(String[] args){
        System.out.println("\f");
        Scanner sc = new Scanner(System.in);
        System.out.println("Hello lets chat | what is your name?");
        System.out.println(">>>");
        String n = sc.nextLine();
        System.out.println("Ok " + n + " How are you?");

        while(true) {
          System.out.println(">>>");
          String s = sc.nextLine();
          s = s.toUpperCase();

          if(s.indexOf("FINE") >= 0) {
            System.out.println("GOOD TO KNOW " + n);
          }

          else if(s.indexOf("HI") >= 0) {
              System.out.println("Hi " + n); 
          }
          else if(s.indexOf("WEATHER") >= 0) {
              System.out.println("I HAVEN'T CHECKED");
          }
          else if(s.indexOf("NAME") >= 0) {
              System.out.println(n + "'s"+" Chatbot");
          }
          else {
              final int num = 3;
              double r = Math.random();
              int whichResponse = (int) (r * num);
              String response ="";

              if(whichResponse == 0) {
                  response = "Hmmm. ";
              }
              else if(whichResponse == 1){
                  response = "Ok...";
              }
              else if(whichResponse == 2){
                  response = "I am not programmed to respond";
              }
              else if(whichResponse == 3){
                  response = "I do not know";
              }
              System.out.println(response);
          }
        }
    }
}


What I have tried:

this code gives me the following error:

java chatbot
Error: Could not find or load main class chatbot
Caused by: java.lang.ClassNotFoundException: chatbot


javac chatbot.java
chatbot.java:28: error: cannot find symbol
          else if(s.indexOf("NAME") >= 0) {
                   ^
  symbol:   method indexOf(String)
  location: variable s of type String


I made sure that everything is in place, but appreciate any suggestions to fix this.
Posted
Updated 13-Apr-21 2:17am

1 solution

What version of Java are you using?
Your code compiles and runs fine on my Linux box
javac chatbot.java 
java chatbot 


Hello lets chat | what is your name?
>>>
Foo
Ok Foo How are you?
>>>
fine
GOOD TO KNOW Foo
>>>
^C


Java versions:
openjdk 11.0.10 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.10+9-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
 
Share this answer
 
Comments
[no name] 14-Apr-21 4:06am    
Thank you for checking and great to know the codes are working! I am pretty new to Java but i think the version is /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home"

I think the problem is the code editor I am using (VSCode). As your Linux fires my codes without inches, I think I messed up initial configurations to set up Java environment with the VSCode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900