Iam trying to shorten these steps
List<String> questionText = [
'You can lead a cow down stairs but not up stairs.',
];
List<bool> questionAnswer = [
false,
Combining these 2 List to single in another class but getting error in 'Question'
class Question {
String questionText;
bool questionAnswer;
Question({required String q, required bool a}) {
questionText = q;
questionAnswer = a;
}
}
//Error Here Error Img
It's written like this in dart.
class Question {
String questionText;
bool questionAnswer;
Question({required this.questionText, required this.questionAnswer});
}
Check: https://dart.dev/guides/language/language-tour#constructors
Create an object like so:
Question question = Question(
questionText: "You can lead a cow down stairs but not upstairs",
questionAnswer: false,
);
Related
I created a task list. I want to count the tasks length like this:
ongoing tasks = task list length - isDone tasks
I want to find the isDone tasks count in this case. As to the picture isDone tasks = 2.
These are the classes that I created.
class Task {
final String taskName;
bool isDone;
Task({required this.taskName, this.isDone = false});
void toggleDone() {
isDone = !isDone;
}
}
........
class Taskdata with ChangeNotifier {
final List<Task> _tasks = [];
UnmodifiableListView<Task> get tasks {
return UnmodifiableListView(_tasks);
}
int get tasksCount {
return _tasks.length;
}
}
You can use the where function to filter a list per a certain condition:
int get tasksCount {
List<Task> tasksLeft = _tasks.where((task) => !task.isDone).toList(); //where returns an iterable so we convert it back to a list
return tasksLeft.length;
}
if something goes wrong, you may need to convert List to Iterable
int get doneCount {
Iterable<Task> tasksDone = _tasks
.where((task) => task.isDone)
.toList(); //where returns an iterable so we convert it back to a list
return tasksDone.length;
}
class Article{
final String id;
final List<ArticleArray> arrays;
}
class ArticleArray {
final String id;
final String array;
}
TextFormField(
onChanged: (_searchinput) {
List<String> searcharray = _searchinput.split(',');
),
_articlesForDisplay = _articles.where((article){
for(int i = 0; i < article.arrays.length; i++)
{
searchinit = article.arrays[i].arrays.toLowerCase();
}
return searchinit.contains(
RegExp(//necessary code here, caseSensitive: false),
);})
basically what i am trying to do is i am trying to search
array by array like:
-Textformfields follows the user input and every time "," is entered,i
put that word into "searcharray".
-Then i initiate a search in my function(total code is not needed to write here,
i included necessary functions),i try to use "regexp" function to search for the arrays
in my article archives.
Example:
articles[0].arrays[0].array = 'a1'
articles[0].arrays[1].array = 'a2'
articles[1].arrays[0].array = 'a1'
articles[1].arrays[1].array = 'b2'
searcharray = ['a1','b2']
Here,basically what i try to do is i try to search my article arrays by every list of arrays as possible so it matches the right one. Thats what i want.
Not sure I understand your specific requirements.
Here, I define a RegExp that I try to match to at least one item of my data.
void main() {
List<String> data = ['a1','b2', 'c3', 'a4', 'a5', 'd6'];
RegExp exp = new RegExp(r"(a\d+)");
print(data.any((item) => exp.hasMatch(item)));
}
This question already has answers here:
How do I create a Stream of regex matches?
(3 answers)
Closed 5 years ago.
I have created a function which return the List.
I am passing a string "(A,1),(Y,4),(F,5)" and I want to split it out based on brackets to get the 3 individual string object which are like A,1, Y,4, F,5 as 3 individual objects in a list.
I used Java 8 to create it but it return me only one value like A,1
The function is:
private List<String> getDataOnRegx(String str) {
Pattern filerRegx = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");
Matcher regexMatcher = filerRegx.matcher(str);
return Stream.of(str).
filter(s -> regexMatcher.find() && regexMatcher.group(1) != null).
map(r -> new String(regexMatcher.group(1))).
collect(Collectors.toList());
}
The expected result I have achieved via below function where I have not used any Java 8 feature:
private List<String> getDataOnRegx(String str) {
Pattern regex = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");
Matcher regexMatcher = regex.matcher(str);
List<String>dataList = new ArrayList<String>();
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
dataList.add(regexMatcher.group(1));
}
}
System.out.println(dataList);
return dataList;
}
Can somebody help me to get the all objects in a list. Just want help to correct my function which I already written using Java 8.
Strictly I have to use Java 8 compiler.
Thanks,
Atul
That's available in java-9 via Scanner#findAll:
String test = "(A,1),(Y,4),(F,5)";
Scanner sc = new Scanner(test);
Pattern filerRegx = Pattern.compile("\\(([a-zA-Z,0-9]*)\\s*\\),?");
List<String> results = sc.findAll(filerRegx)
.map(mr -> mr.group(1))
.filter(Objects::nonNull)
.collect(Collectors.toList());
System.out.println(results);
EDIT
I have no idea how that duplicate answer does not answer your question. There's like a very simple change you need to make:
static final class MatchItr extends Spliterators.AbstractSpliterator<String> {
private final Matcher matcher;
MatchItr(Matcher m) {
super(m.regionEnd() - m.regionStart(), ORDERED | NONNULL);
matcher = m;
}
#Override
public boolean tryAdvance(Consumer<? super String> action) {
if (!matcher.find()) {
return false;
}
if (matcher.group(1) == null) {
return false;
}
action.accept(matcher.group(1));
return true;
}
}
And use it:
MatchItr mIter = new MatchItr(regexMatcher);
StreamSupport.stream(mIter, false)
.forEach(System.out::println);
I'm new to grails 1.3.7 and I try to access my database and to show my data on a gsp. Now Ive got the following problem: I've got a list of questions (listofQuestions) and a list of answers (listofAnswers). To each question belongs one Lpicanswer object which contains various answers (answera, answerb)
So when I create those lists, in the end I've got one list containing the questions and one list containing lpicanswer-objects. each lpicanswerobject has an lpicid (which is the id of the question), so that they are related to each other.
Here is the code to create those lists:
List listofQuestions = []
List listofAnswers = []
def ques
def question
def ans
// we create a questions list containing questions
// we create a answers list containing answers-objects for a question
for (int i = 0; i <= cacheService.questionList.size()-1; i++) {
ques = Lpicquestions.get(cacheService.questionList[i]);
question = ques.question;
listofQuestions.add(question);
}
for (int i = 0; i <= cacheService.questionList.size(); i++) {
ans = Lpicanswers.get(cacheService.questionList[i]);
listofAnswers.add(ans);
}
return new ModelAndView("/result/resultdetail", [ qlist : listofQuestions, alist : listofAnswers ]);}
now I want to show them on my gsp. here is what I do:
<g:each in="${qlist}">
<b>${it}</b><br/>
${alist.answera}<br/>
${alist.answerb}<br/>
${alist.answerc}<br/>
${alist.answerd}<br/>
${alist.answere}<br/>
${alist.answerf}<br/>
${alist.answerg}<br/>
${alist.answerh}<br/>
</g:each>
what happens is, that the questions are given out correct, but the answers of course not. For each question all answersa, all answersb, etc are shown (like: [answera-from-question1, answera-from-question2] and so on) how can I solve this?
any help will be apreciated! :-)
[EDIT] Here is the structure of lpicquestions and lpicanswers, thanks for helping!! :-)
package com.lpic
class Lpicquestions {
int lpicchapter
String question
static constraints = {
question(nullable:false, blank:false, maxSize:1000)
lpicchapter(nullable:false, blank:false)
}
}
package com.lpic
class Lpicanswers {
Lpicquestions lpicid
String answera
String answerb
String answerc
String answerd
String answere
String answerf
String answerg
String answerh
static constraints = {
}
}
aList is not an object or map. So you can't put something like:
${alist.answera}
change the view to.
<g:each var="question" in="${qlist}">
<b>${question}</b><br/>
<g:each var="answer" in="${aList}">
<g:if test="${answer.lpicid?.question == question}">
<b>${answer.answera}</b><br/>
<b>${answer.answerb}</b><br/>
<b>${answer.answerc}</b><br/>
<b>${answer.answerd}</b><br/>
<b>${answer.answere}</b><br/>
<b>${answer.answerf}</b><br/>
<b>${answer.answerg}</b><br/>
<b>${answer.answerh}</b><br/>
</g:if>
</g:each>
</g:each>
if assuming that cacheService.questionList contains list of id for Lpicquestions
change
for (int i = 0; i <= cacheService.questionList.size(); i++) {
//ans = Lpicanswers.get(cacheService.questionList[i]);
ans = Lpicanswers.findWhere(['lpicid' : Lpicquestions.get(cacheService.questionList[i])]);
listofAnswers.add(ans);
}
Problem: I can't access the count of items in a SelectList
I have an HtmlHelper method that returns a SelectList:
public static SelectList FilterSelectList(this HtmlHelper helper, List<Stuff> eList, string dept)
{
List<Stuff> returnList = new List<Stuff>();
//Do Stuff
return new SelectList(returnList, "ID", "Name");
}
I then have a test which confirms that the filter was done correctly:
// Arrange
List<Stuff> eList = MVCMocks.GetList();
string dept = "T";
int expectedCount = eList.FindAll(e => e.Dept == dept).Count;
// Act
var actual = HtmlHelpers.FilterSelectList(helper, eList, dept);
// Assert
Assert.AreEqual(expectedCount, actual.Count, "The list was not properly filtered.");
Calling actual.Count results in an error.
I'm hoping this is just a case of me having a stupid oversight, but I've been banging my head on this for a while. Prove me right! :)
EDIT: Stuff I've Tried
actual.Count
actual.Count()
actual.Items.Count()
actual.GetEnumerator().?
You need actual.Count() (note parens!) not actual.Count.