简体   繁体   English

如何检查单词是否包含一个字母或一组字母?

[英]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. 我正在为我的CompSci I类编写程序,但我完全陷入困境。 The assignment is to use if statements to check whether a particular string contains a letter or group of letters. 分配是使用if语句检查特定字符串是否包含一个字母或一组字母。 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. 我真的是Strings和Return方法的新手。 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: 到目前为止,我的代码:Main:

import static java.lang.System.*;

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

}
}

StringChecker: 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. 您可以简单地使用String类的contains方法,该方法已经实现了所需的行为。 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. 因此,一种基本方法是遍历String中的所有字符并与目标字符进行比较。 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. 您可以使用length()方法获得String的长度,而charAt(int)方法将为您提供给定位置的字符。 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() : 如果不允许使用String.contains()

You have to loop over every character in your String. 您必须遍历String中的每个字符。 You can use String.charAt(int i) to get the single character at position i. 您可以使用String.charAt(int i)获取位置i处的单个字符。

To check for a subString you have to loop over both Strings. 要检查subString,您必须遍历两个String。

With the if-statement you then just check if two characters are equal. 然后,使用if语句检查两个字符是否相等。 In this case you have found an occurrence. 在这种情况下,您发现了一个事件。

I believe what you are looking for is the String class . 我相信您正在寻找的是String类 In particular you're looking at the .contains(CharSequence s) method. 特别是,您正在查看.contains(CharSequence s)方法。 Below, is an example of how you may do this for your scenario (using if statements) 以下是如何针对您的方案执行此操作的示例(使用if语句)

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 . 一种简单的方法是使用String Classcontains方法。 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. 如果您想手动执行此操作,请使用substring方法检查所有可能的子字符串,该子字符串是您要查找的String的长度。 Use the equals method for comparison. 使用equals方法进行比较。 Be sure that when iterating over the String it does extend farther than the last index. 请确保在对String进行迭代时,它的扩展确实比最后一个索引远。

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). 但是main也可以与类定义位于同一文件中(但只能在一个类定义文件中,因为只能有一个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. return是语句,而不是方法。 Important to know the difference. 重要的是要知道区别。

Also, in Java, you can initialize your variables statically in the class definition. 另外,在Java中,您可以在类定义中静态初始化变量。 You don't need to do it in the constructor for your simple case, you could write String check = "" in the definition. 对于简单的情况,您不需要在构造函数中执行此操作,可以在定义中编写String check =“”。

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(). 我曾经做过... 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. 这种循环形式通常不是优选或不需要的,并且比在顶部显示条件的for循环更难于阅读和维护代码。 You usually can (and generally should) find a way to structure your loops into a form that works with for() rather than do/while. 通常,您可以(通常应该)找到一种方法来将循环结构化为与for()一起使用的形式,而不是do / while。 You can usually efficiently arrange loops into for() format through use of boolean flags or null checks. 通常,您可以使用布尔标志或null检查将循环有效地安排为for()格式。

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) 此示例不考虑任意长度的子字符串(因为我们使用的是if语句,因此对于长子字符串,它将变得非常长,而如果处理可变长度子字符串,则将变得非常复杂)

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). 导致此示例格式错误的另一个问题是,它没有创建处理整个代码中重复出现的事情的函数(例如,检查值并打印语句的函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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