Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Intro to Java

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Intro to Java

    I haven't used any programming in 20 years and back then it was Basic.

    I am trying to write an (extremely) simple chat bot program just to get used to if/then with the scanner.

    One thing I can't figure out how to do is read the user input and check for two words. In my example (line 20) I am trying to find the word "hello" and the word "there". If the input contains both words it will return "success". I've gotten this to work to some degree if "hello" and "there" are adjacent to each other but I want a positive return if they exist anywhere in the user's input, for example: "hello, what are you doing there?" or "hello there"

    Additionally, something I haven't learned yet is how to make the code loop back up to the top after the user submits data rather than the program terminating.

    Be simple with me. I am to learn basics. Thank you so much!

    package chatBot;
     
    import java.util.Scanner;
     
    public class chatBotClass {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
     
     
     
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Program begins");
    		String uInput = scan.nextLine(); //next will find the next word the user enters
     
     
     
     
    		if (uInput.contains("hello") + uInput.contains("there")) {
    		System.out.println("success");
     
    		}
     
     
    		else if (uInput.equals("hi")) 
    		{
    			System.out.println(uInput +"!");
    		}
     
    		else if (uInput.equals("Hello")) {
    			System.out.println(uInput);
    		}
     
     
     
     
    		else
    		{
    		System.out.println("NO WORD ERROR");}
     
    }
     
    	private static String contains(String string) {
    		return null;
    	}
     
     
     
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Intro to Java

    Here's a good resource to have while learning java: http://docs.oracle.com/javase/tutori...ybigindex.html

    Look at while loops for your problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java intro
    By Stealthrt in forum Member Introductions
    Replies: 1
    Last Post: April 9th, 2019, 12:36 PM
  2. Intro to all java geeks
    By java_avaj in forum Member Introductions
    Replies: 1
    Last Post: April 27th, 2014, 06:28 AM
  3. Intro to Java
    By omen1090 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 07:37 AM
  4. Intro to java assignment help
    By Rahiant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2012, 01:05 PM
  5. Intro to java hw assignment
    By coke32 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 07:45 AM