2014-12-15 2 views
3

스캐너를 통해 입력 된 수의 문자열 (int T)을 입력 받아 arraylist에 저장하는 프로그램을 작성하고 싶습니다. 그런 다음 입력을 검사하여 일치하는지 또는 다른 배열의 문자가 포함되어 있는지 확인하려고합니다.Arraylist.contains 수표 문자열이 없습니다.

예 입력 :

1 
ABCD

예 출력 :

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

    } 

} 
+1

오류가 발생한 행이 있습니까? –

+0

어디에서 발생했는지 보여줄 수 있습니까? – Marv

+0

문자열 개체를 확인하고있을 수 있습니까? 왜냐하면 문자열 객체는 값이 같을 수 있지만 참조가 아니라는 점에서 다소 까다 롭습니다. – JClassic

답변

3

커플처럼 보일 수 있습니다 :

  • 기본적으로 입력 불일치 예외가 발생하는 q.nextInt(); 대신 새 줄 문자를 사용하려면 q.next();을 사용해야합니다.
  • list.contains(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 
        } 
    } 
    
0

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 
    } 
} 
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()을 사용하여 재설정하십시오.

0

nextInt()와 nextLine()에 동일한 스캐너를 사용하지 마십시오. 그들은 버그 아웃. int 용과 string 용의 2 개의 스캐너를 사용하십시오. 이렇게하면 프로그램이 수정됩니다.