Arraylist.contains不会检查字符串

时间:2014-12-15 16:13:56

标签: java arraylist iterator java.util.scanner

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

示例输入:

1
ABCD

示例输出:

Good

问题:当我运行代码时,我没有得到" Good"或者"坏"输出,而不是我得到一个错误,调试控制台启动。

确切错误:

Scanner.throwFor() line: not available. Source not found
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
            }
        }

    }

}

4 个答案:

答案 0 :(得分:3)

几个问题:

  • 您应该使用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
       }
    }
    

答案 1 :(得分:0)

检查A是否包含字符串数组(直接是String[]对象),而不是它是否包含此数组中的字符串(此String[]内的内容)。

将其替换为

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

所以你有一个Array的List,然后使用contains

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
  }
}

答案 2 :(得分:0)

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 :(得分:0)

不要对nextInt()和nextLine()使用相同的扫描程序。他们错了。使用两个扫描仪,一个用于整数,一个用于字符串。这将修复你的程序。