简体   繁体   中英

How do I check to see if a word contains a letter or group of letters?

I am working on a program for my CompSci I class and I'm entirely stuck. The assignment is to use if statements to check whether a particular string contains a letter or group of letters. I have a class created for this and I've laid out everything, but I just don't know how to begin searching for the characters. I'm really new to Strings and Return methods. I looked for similar questions already, but none really helped with what I was looking for. I would appreciate some help.

My code so far: Main:

import static java.lang.System.*;

public class Lab04e
{
public static void main(String[] args)
{

}
}

StringChecker:

import static java.lang.System.*;
public class StringChecker {

private String check;

    public void StringChecker()
    {
        check = "";
    }

    public void StringChecker(String s) //constructor
    {
        check = s;
    }

    public void setString(String s) //set string
    {
        check = s;
    }

    public boolean letterExists(char a)
    {
        return false;
    }

    public boolean findSubString(String s)
    {
        return false;
    }

    public String toString()
    {
        return check +"\n\n";
    }
}

You could simply use the contains-method of the String-class, that already implements the desired behaviour. But I assume, the point in this assignment is learning HOW to do it. So a basic approach is to iterate over all the characters in the String and compare with your target character. You can get the length of the String with the method length() and the method charAt(int) gives you the character at the given position. So it could look like this:

for (int i = 0; i < check.length(); i++() {
   if (check.charAt(i) == a) return true;
}
return false;

You can expand starting from that to check for substrings.

If you are not allowed to use String.contains() :

You have to loop over every character in your String. You can use String.charAt(int i) to get the single character at position i.

To check for a subString you have to loop over both Strings.

With the if-statement you then just check if two characters are equal. In this case you have found an occurrence.

I believe what you are looking for is the String class . In particular you're looking at the .contains(CharSequence s) method. Below, is an example of how you may do this for your scenario (using if statements)

boolean characterCheckString = false;
String myString = "abcde";
if(myString.contains("a")) {
    characterCheckString = true;
}

Read more on this method here .

An easy way of doing it would be to use the contains method of the String Class . If you would like to do it manually, use the substring method to check every possible substring that is the length of the String you are looking for. Use the equals method for comparison. Be sure that when iterating over the String it does extend farther than the last index.

I've provided a crude working example (it compiles) You can adapt it to a multi-file main + class situation as desired. But main can also be in the same file as a class definition (but only in one of your class definition files, as there can only be one main).

You can compile it as:

javac StringChecker.java

You can test it on the command line with something like:

java StringChecker stringxyztocheck

This is the source code:

import java.lang.System.*;
import java.util.*;

public class StringChecker {

    public static void main (String[] args) {
        String fullString = args[0];
        String subString = "xyz";

        if (fullString.length() < subString.length()) {
            System.out.println("'" + subString + "' not found in '" + fullString + "'");
            System.exit(0);
        }
        int i = 0;
        do {
            if (fullString.charAt(i) == subString.charAt(0) &&
                fullString.charAt(i + 1) == subString.charAt(1) &&
                fullString.charAt(i + 2) == subString.charAt(2)) {
                   System.out.println("'" + subString + "' is present in '" + fullString + "'");
                   System.exit(0);
            }
       } while (i++ < fullString.length() - subString.length());

        System.out.println(subString + "not found in '" + fullString + "'");
        System.exit(0);
    }
} 

OK, now for the lecture :-)

You said "Return method". return is a statement, not a method. Important to know the difference.

Also, in Java, you can initialize your variables statically in the class definition. You don't need to do it in the constructor for your simple case, you could write String check = "" in the definition.

The above example is crude and of bad form. I'd never write a real world program this way, it's just proof of concept. So I'm avoiding using flags, variables, functions and loops as much as possible.

I used do... while(). That form of looping is generally not preferred or needed and makes the code harder to read and maintain than a for loop that shows the condition at the top. You usually can (and generally should) find a way to structure your loops into a form that works with for() rather than do/while. You can usually efficiently arrange loops into for() format through use of boolean flags or null checks.

This example doesn't account for an arbitrary length sub-string (because we're using an if statement, so it would get extremely long for long sub-strings, and extremely complex if statements to handle variable length sub-strings)

Another issue that makes this example bad form is that it doesn't create a function to handle things that are repeated throughout the code (such as a function that checks a value and prints a statement).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM