简体   繁体   English

Java,如何将字符串与String Arrays进行比较

[英]Java, how to compare Strings with String Arrays

I have been searching here for some time but haven't been able to find the answer to it. 我一直在这里搜索一段时间,但一直未能找到答案。

I am basically required to use an array for this assignment from college. 我基本上需要使用数组来完成大学的这项任务。 And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array. 然后我应该检查输入(也是一个String)匹配String数组中存储的内容。

I know one can easily compare Strings by using the .equals() method. 我知道可以通过使用.equals()方法轻松比较Strings。 However, the same method is not working with the String array. 但是,相同的方法不适用于String数组。

I created the following example of code for the purpose of StackOverflow so you can use it to explain it to me, if you'd like. 我为StackOverflow创建了以下代码示例,因此您可以使用它来向我解释它,如果您愿意的话。

What am I doing wrong? 我究竟做错了什么?

import java.util.Scanner;

class IdiocyCentral {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        /*Prints out the welcome message at the top of the screen*/
        System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
        System.out.printf("%55s", "=================================\n");

        String [] codes = {"G22", "K13", "I30", "S20"};

        System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
        System.out.printf("Enter one of the above!\n");

        String usercode = in.nextLine();

        if (codes.equals(usercode)) {
            System.out.printf("What's the matter with you?\n");
        }
        else {
            System.out.printf("Youda man!");
        }

    }
}

I apologize if this has been asked before and I just missed it, if its a double question, I will remove it. 如果以前曾经问过这个问题,我很抱歉,如果它是一个双重问题,我会将其删除。

I presume you are wanting to check if the array contains a certain value, yes? 我认为你想要检查数组是否包含某个值,是吗? If so, use the contains method. 如果是这样,请使用contains方法。

if(Arrays.asList(codes).contains(userCode))

Right now you seem to be saying 'does this array of strings equal this string', which of course it never would. 现在你似乎在说'这个字符串数组是否等于这个字符串',这当然不会。

Perhaps you should think about iterating through your array of strings with a loop, and checking each to see if they are equals() with the inputted string? 也许您应该考虑使用循环遍历您的字符串数组,并检查每个字符串是否与输入的字符串是equals()?

...or do I misunderstand your question? ......还是我误解了你的问题?

Iterate over the codes array using a loop, asking for each of the elements if it's equals() to usercode . 使用循环迭代codes数组,询问每个元素是否equals()到用户usercode If one element is equal, you can stop and handle that case. 如果一个元素相等,则可以停止并处理该情况。 If none of the elements is equal to usercode , then do the appropriate to handle that case. 如果所有元素都不等于用户usercode ,那么请执行相应的操作以处理该情况。 In pseudocode: 在伪代码中:

found = false
foreach element in array:
  if element.equals(usercode):
    found = true
    break

if found:
  print "I found it!"
else:
  print "I didn't find it"
import java.util.Scanner;
import java.util.*;
public class Main
{
  public static void main (String[]args) throws Exception
  {
    Scanner in = new Scanner (System.in);
    /*Prints out the welcome message at the top of the screen */
      System.out.printf ("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
      System.out.printf ("%55s", "=================================\n");

      String[] codes =
    {
    "G22", "K13", "I30", "S20"};

      System.out.printf ("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2],
             codes[3]);
      System.out.printf ("Enter one of the above!\n");

    String usercode = in.nextLine ();
    for (int i = 0; i < codes.length; i++)
      {
    if (codes[i].equals (usercode))
      {
        System.out.printf ("What's the matter with you?\n");
      }
    else
      {
        System.out.printf ("Youda man!");
      }
      }

  }
}

If I understand your question correctly, it appears you want to know the following: 如果我正确理解您的问题,您似乎想知道以下内容:

How do I check if my String array contains usercode , the String that was just inputted? 如何检查我的String数组是否包含用户usercode ,刚刚输入的String

See here for a similar question. 在这里查看类似的问题。 It quotes solutions that have been pointed out by previous answers. 它引用了之前答案中指出的解决方案。 I hope this helps. 我希望这有帮助。

而不是使用数组,你可以使用ArrayList直接可以使用contains方法来检查哪些u必须与传递价值ArrayList

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

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