I am currently working on a method which outputs into a list all the subject codes which contain the string input by the user (s). For example, my ArrayList has 4 books which have two fields: name, subjectCode. Currently, the output for the method returns // True, False, False, True, etc. How do i configure it so it displays the subjectCode for all the True values eg. COMM100, COMM200 when input s = COM
private static void findSubjectCodes(ArrayList<Subject2> list, String s) {
System.out.println("Subject codes found:");
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i).subjectCode.contains(s) + "\n");
// True, False, True, etc
}
Put your contains
condition into a conditional branch. eg
for (int i = 0; i < list.size(); i++) {
String subjectCode = list.get(i);
if (subjectCode.contains(s) {
System.out.prinltn(subjectCode + "\n");
}
}
Return type of .contains(*)
is boolean, that is why you are getting boolean value as output.
It is basically checking whether the said attribute contains the received text , hence list.get(i).subjectCode.contains(s)
is printing a boolean value.
To print the subject code you need to do something like this:
for (int i = 0; i < list.size(); i++){
if(list.get(i).subjectCode.contains(s))
{
System.out.println("Subject is found:" +list.get(i).subjectCode);
}
else{
System.out.println("Subject not found.");
}
}
Here is the official documentation : https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.