Arraylist.contains does not validate string
I want to create a program that inserts a given number of lines ( int T
) through a scanner and stores them inside an arraylist. Then I want to check the input to see if it matches or contains characters from another array.
Input example:
1 ABCD
Result:
Good
Problem: When I run the code, I don't get "good" or "bad" output, instead I get an error and it launches the debug console.
The exact error:
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
}
}
}
}
source to share
A couple of problems:
- You should use
q.next();
to use a new line character insteadq.nextInt();
, which is basically what you get an input mismatch exception. -
You are running
list.contains(Array)
which is not supported. If you want to check every user input, regardless of whether it is in array X, you should probably do something like: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 } }
source to share
You are checking if A contains a string array (directly an object String[]
), not if it contains a string inside that array (what is inside that String[]
).
Replace it
List<String> result = Arrays.asList(X);
So you have a list from your array and then use
result.contains(A)
Your code might look like:
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
}
}
source to share
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();
}
}
}
Try it.
instead q.nextInt()
use q.nextLine()
before reset.
source to share