Java 在一个字符串数组中搜索另一个字符串数组

Java 在一个字符串数组中搜索另一个字符串数组,java,arrays,string,Java,Arrays,String,我刚开始写java,有一个问题我必须在大学完成。 到目前为止,我已经完成了问题要求我做的所有事情,除了最后一部分,我必须在其中键入一个名称,如果名称与字符串数组中的名称匹配,它将计算值。已在上面停留了2个小时,请帮助 System.out.println(); System.out.print("Enter the candidate's name: "); candi[0] = keyIn.nextLine(); for(int i = 0; i<votes.

我刚开始写java,有一个问题我必须在大学完成。 到目前为止,我已经完成了问题要求我做的所有事情,除了最后一部分,我必须在其中键入一个名称,如果名称与字符串数组中的名称匹配,它将计算值。已在上面停留了2个小时,请帮助

      System.out.println();
  System.out.print("Enter the candidate's name: ");
  candi[0] = keyIn.nextLine();


  for(int i = 0; i<votes.length; i++)
  {
     if(candi[0].equals(names[i]))
     {

        for(int h = 0; h<votes.length; h++)
           {

              if(votes[i] == votes[h])
              {
                 same = same+1;
              }
              else if(votes[i]<votes[h])
              {
                 less = less+1;
              }
              else if(votes[i]>votes[h])
              {
                 more = more + 1;
              }
           }
     }
     else
     {

           System.out.print("Name does not match, enter again : ");
           candi[0] = keyIn.nextLine();
     }
  }
System.out.println();
System.out.print(“输入候选人姓名:”);
candi[0]=keyIn.nextLine();

对于(int i=0;i您的问题是,您只为每个输入检查一个名称(名称位于
i
)。您需要遍历所有名称,以检查输入的名称是否与数组中的名称匹配。为了解决此问题,我将整个内容包装在
while
循环中,并将匹配检查分离到自己的小
for
循环:

    String[] names = {"Dunne","Doherty","McGlynn","Grant","Sweeney","McHugh","Gibbons","O'Neill"}; 
    int[] votes = {0,13,28,6,6,29,15,5}; 
    double[] avg = {0.0,12.7,27.5,5.9,5.9,28.4,14.7,4.9};

    System.out.println();
    System.out.print("Enter the candidate's name: ");
    Scanner scan = new Scanner(System.in);
    String in = scan.nextLine();

    boolean containsName = false;

    while (true) { // continue asking for names until one mathces

        // Check if name is present
        for(int i = 0; i<names.length; i++)
        {
            if(in.equals(names[i]))
            {
                containsName = true;
                break; // break the for loop
            }
        }

        // do something if name is present, continue if not
        if(containsName) {
            System.out.println("Calc!");
            break; // break the while loop
        }else {
            System.out.print("Name does not match, enter again : ");
            in= scan.nextLine();
        }


    }
String[]name={“Dunne”、“Doherty”、“McGlynn”、“Grant”、“Sweeney”、“McHugh”、“Gibbons”、“O'Neill”};
int[]票={0,13,28,6,6,29,15,5};
双[]平均值={0.0,12.7,27.5,5.9,5.9,28.4,14.7,4.9};
System.out.println();
System.out.print(“输入候选人姓名:”);
扫描仪扫描=新扫描仪(System.in);
字符串in=scan.nextLine();
布尔containsName=false;
while(true){//继续询问姓名,直到找到一个匹配项
//检查名称是否存在

对于(inti=0;i我想您正在寻找这样的东西:

当找到用户输入的名称时,可以在其中执行某些操作 在候选名称数组中

工作代码:

import java.util.Scanner;

class match {
    match() {
        Scanner keyIn = new Scanner(System.in); //for taking input
        String candi[] = new String[1];         //for storing name input
        String candiNames[] = { "ab", "bc", "cd" }; //String array of candidate names
        Boolean found = false; //flag to check if name found or not

        System.out.print("Enter the candidate's name: ");
        candi[0] = keyIn.nextLine(); //taking input

        for (String n : candiNames) { //looping 
            if (candi[0].equalsIgnoreCase(n)) { //matching string (ignoreing case)

                found = true;
                // do the working here for anything you wish to do on match
                // found
            }

        }
        if (found == true) {
            System.out.println("Match found ");

        } else if (found == false) {
            System.out.println("Match not found for : " + candi[0]);
        }
    }

}

public class StringName {
    public static void main(String args[]) {
        new match();
    }
}
输出:

  • 找到匹配项

  • 未找到匹配项

我希望我能帮忙:)


您得到的错误和期望的o/p是什么?它将始终转到else语句,我已经输入了单词在数组中的确切方式,我不确定o/p的意思是什么。所以/p的意思是“输出”不应该是第一个for循环,直到names.length而不是vowers.length?另外,您也可以发布names数组吗?请尝试使用trim()类似于if条件:candi[0].trim().equals(名称[i].trim())