Search code examples
javastringbluejsystem.out

BlueJ string issue


I'm trying to create a program in BlueJ that allows the reader to type any word, then print out: that word, the length of the word, and whether or not the word contains "ing". I've figured out how to print the word and its length, but I can't figure out how to include whether "ing" is in the word.

Here is my code:

import java.util.*;
public class One
{
public static void main(String[] args)  
{
    Scanner sc = new Scanner(System.in);
    String str = "";
    System.out.println("Type in a word");
    str = sc.nextLine();
    //System.out.println(str);
    System.out.println(str.length());

 }
}

How can I tell whether "ing" is included in the word?


Solution

  • You can do that using the contains() method on your resulting string:

    if (str.contains("ing")) System.out.println("Contains ING");
    

    If you need to match either lowercase or uppercase, you can just convert str to upper case and check that instead:

    if (str.toUpperCase().contains("ING")