Java 一个程序,它提取一个单词,然后搜索并打印出包含该单词的整个字符串

Java 一个程序,它提取一个单词,然后搜索并打印出包含该单词的整个字符串,java,Java,该程序的基本功能是获取一个单词并在字符串数组中搜索,而不是搜索并打印包含该单词的字符串 public static void main(String[] args) { String[] str = {"I am Alive.", "Are you dead?", "Let's see if it works."}; String search; int count=0; Scanner s=new Scanner(System.in); System.o

该程序的基本功能是获取一个单词并在字符串数组中搜索,而不是搜索并打印包含该单词的字符串

public static void main(String[] args) {
    String[] str = {"I am Alive.", "Are you dead?", "Let's see if it works."};
    String search;
    int count=0;
    Scanner s=new Scanner(System.in);
    System.out.println("enter word");
    search=s.nextLine();

    for(int i=0;i<str.length;i++){
        //check strings individually
        if(str[i].charAt(i)=='.'||str[i].charAt(i)=='?'){   //search for dot or any sentence finisher
            count++;
        }
        if(str[i].contains(search)){
            System.out.println(str[count]);
        } else {
            System.out.println("Not found");
            break;
        }
    }
}
publicstaticvoidmain(字符串[]args){
String[]str={“我还活着”,“你死了吗?”,“让我们看看它是否有效。”};
字符串搜索;
整数计数=0;
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入单词”);
search=s.nextLine();

对于(inti=0;i注意
中断
,它总是执行的

选中此项:

public static void main(final String... args) {    
  final String[] strings = { "I am Alive.", "I am Alive...too.", "Are you dead?",
      "Let's see if it works." };
  int found = -1;

  System.out.print("Enter search term > ");
  try (final Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name())) {
    final String input = scanner.nextLine();

    for (int i = 0; (i < strings.length) && (found < 0); i++) {
      if (strings[i].contains(input) && (strings[i].contains(".") ||
          strings[i].contains("?"))) {
        //System.out.println(strings[i]);
        found = i;
      }
    }
  }
  System.out.printf("%s%n", (found >= 0) ? strings[found] : "Not found");
}
publicstaticvoidmain(最终字符串…args){
最后一个字符串[]字符串={“我还活着”,“我也还活着”,“你死了吗?”,
“让我们看看它是否有效。”};
int-found=-1;
系统输出打印(“输入搜索项>”;
try(最终扫描仪=新扫描仪(System.in,StandardCharsets.UTF_8.name()){
最终字符串输入=scanner.nextLine();
对于(int i=0;(i=0)?字符串[已找到]:“未找到”);
}

你不应该在那里打断循环,因为只有在你遍历了数组中的所有句子后,才会认为它没有找到。类似这样:

public static void main(String[] args) {
    String[] str={"I am Alive.","Are you dead?","Let's see if it works."};
    String search;

    Scanner s=new Scanner(System.in);
    System.out.println("Enter word");
    search=s.nextLine();

    boolean found = false;
    for(int i=0;i<str.length;i++){
        if(str[i].contains(search)){
            System.out.println(str[i]);
            found = true;
        }
    }

    if (!found) {
        System.out.println("Not found");
    }
}
publicstaticvoidmain(字符串[]args){
String[]str={“我还活着”,“你死了吗?”,“让我们看看它是否有效。”};
字符串搜索;
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入单词”);
search=s.nextLine();
布尔值=false;

对于(int i=0;i您知道
break
keyworkd的作用吗?如果它在第一个数组元素中不可用,它将停止循环并打印
not found
。在这种情况下,我建议使用
布尔值
而不是
int
。它可以工作,但我无法理解StandardCharset.UTF_8.name()是什么我在这里咨询过,但没有帮助much@UmaKanth我正在根据找到的
打印结果;它是(更多)从语义上讲,使用
布尔值是正确的,因为
-1
0
以及所有这些东西都容易混淆,但在本例中它很方便。最初,我使用的是
布尔值
,但因为我想在
for循环
之外打印结果,“found element index”在该范围内不再可用…但有一点很好!@ahsanarif您可以指定(或不指定)要与
Scanner
一起使用的字符集,并且流中的字节将使用指定的字符集转换为字符;由于我总是使用UTF-8(除非我不能),我倾向于指定它。请查看