【问题标题】:Arraylist.contains won't check stringArraylist.contains 不会检查字符串
【发布时间】:2015-02-13 19:15:21
【问题描述】:

我想构建一个程序,该程序通过扫描仪输入一组字符串 (int T),并将它们存储在数组列表中。然后我想检查输入以查看它是否匹配或包含来自另一个数组的字符。

示例输入:

1
ABCD

示例输出:

好

问题:当我运行代码时,我没有得到“好”或“坏”的输出,而是出现错误并启动调试控制台。

确切的错误:

Scanner.throwFor() 行:不可用。找不到来源
import java.io.*; 
import java.util.*;

public class RNA {

    public static void main(String[] args) {

        String X [] = {"A", "B", "C", "D"};  // Array to be checked against


        List<String>A = new ArrayList();  // ArrayList to be imported

        Scanner q = new Scanner(System.in);

        System.out.println("How Many Sets of Strings do you want?");

        int T = q.nextInt(); // number of Strings to be imported
        q.nextInt();  // allows to reset Scanner

        for(int i = 0 ; i < T; i ++){

            A.add(q.nextLine());  //imports stuff to add to array A
        }


        Iterator<String> ListChecker = A.iterator(); 

        while((ListChecker.hasNext())) {   //continues as long as DNA Check has an index to go to 

            if (A.contains(X)) {                  //Checks A for X
                System.out.println("Good");       //Prints out good if the check is good
            }
            else {

                System.out.println("Bad");        //Prints out bad if the check is bad
            }
        }

    }

}

【问题讨论】:

  • 它是否为您提供了发生错误的行?
  • 你能告诉我们它发生在哪里吗?
  • 可能是您正在检查字符串对象吗?因为字符串对象有点挑剔,因为值可能相等但引用不相等。
  • 我认为你应该检查数组的元素。所以,它应该是 if (A.contains(X[0]))。当然应该检查数组的每个元素
  • 你检查 A 是否包含一个字符串数组(直接是 String[] 对象),而不是它是否在这个数组中包含一个字符串(这个 String[] 里面是什么)。您的代码一团糟(使用更好的名称等来改进)。

标签: java arraylist iterator java.util.scanner


【解决方案1】:

切勿对 nextInt() 和 nextLine() 使用相同的扫描仪。他们出故障了。使用两台扫描仪,一台用于整数,一台用于字符串。这将修复您的程序。

【讨论】:

    【解决方案2】:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Scanner;
    
    public class RNA {
    
        public static void main(String[] args) {
    
            String X [] = {"A", "B", "C", "D"};  // Array to be checked against
    
    
            List<String>A = new ArrayList<String>();  // ArrayList to be imported
    
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("How Many Sets of Strings do you want?");
    
            int T = scanner.nextInt(); // number of Strings to be imported
    
            scanner.nextLine();
    
            for(int i = 0 ; i < T; i ++) {
                String nextLine = scanner.nextLine();
                A.add(nextLine);  //imports stuff to add to array A
            }
    
            scanner.close();
    
            Iterator<String> ListChecker = A.iterator(); 
            while((ListChecker.hasNext())) {   //continues as long as DNA Check has an index to go to 
    
                if (A.contains(X)) {                  //Checks A for X
                    System.out.println("Good");       //Prints out good if the check is good
                }
                else {
    
                    System.out.println("Bad");        //Prints out bad if the check is bad
                }
    
                ListChecker.next();
            }
    
        }
    
    }
    

    试试这个。

    而不是q.nextInt() 使用q.nextLine() 重置。

    【讨论】:

      【解决方案3】:

      你检查 A 是否包含一个字符串数组(直接是 String[] 对象),而不是它是否在这个数组中包含一个字符串(这个 String[] 里面是什么)。

      替换为

      List<String> result = Arrays.asList(X);
      

      所以你有一个来自数组的列表,然后使用包含

      result.contains(A)
      

      您的代码可能如下所示

      List<String> xAsList = Arrays.asList(X);
      
      // [....]
      
      while((ListChecker.hasNext())) {   //continues as long as DNA Check has an index to go to
        if (xAsList.contains(ListChecker.next())) { //Checks A for X
          System.out.println("Good");       //Prints out good if the check is good
        } else {
          System.out.println("Bad");//Prints out bad if the check is bad
        }
      }
      

      【讨论】:

        【解决方案4】:

        几个问题:

        • 您应该使用q.next(); 来使用换行符而不是q.nextInt();,这基本上是您遇到输入不匹配异常。
        • 您正在做的list.contains(Array) 不受支持。如果您希望检查用户的每个输入是否在 Array X 中,那么您可能应该执行以下操作:

          List<String> list = Arrays.asList(X);
          while((ListChecker.hasNext())) {   //continues as long as DNA Check has an index to go to 
             if (list.contains(ListChecker.next())) {                  //Checks A for X
                 System.out.println("Good");       //Prints out good if the check is good
             } else {
                 System.out.println("Bad");        //Prints out bad if the check is bad
             }
          }
          

        【讨论】:

          猜你喜欢
          • 2013-03-24
          • 1970-01-01
          • 2015-08-26
          • 1970-01-01
          • 2011-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多