How to iterate a class? - list

UPDATES
This is my final codes just in case anyone needs it:
int index = -2; //I am not 100% sure why I need to start -2, but I assume that `forEach((item){})` probably increase `index` by one, and I also increase `index` inside of the loop, so that's probably why.
recyclable.forEach((item) {
index++;
if (item.title == _outputs[0]["label"]) {
//your code for when the match is found
//move to the detailed page to show more description
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailScreen(recyclable: recyclable[index]),
),
);
}
}
END OF UPDATES
I created a class named Recyclable, and using the class, I created a list named recyclable. The list recyclable has a string named title, and I am trying to iterate that title to find a match with _outputs[0]["label"].
To do it, I tried the following code:
while (_outputs[0]["label"] != recyclable[index].title) {
index++;
}
Somehow, there was a red underline for index, which I have no idea why.
I also tried for loop as below to remove that red underline by removing index from my code:
for (var _outputs[0]["label"] in recyclable.title) {
index++;
}
But the code seems to be completely off.
FYI, Here is my class Recyclable:
class Recyclable {
final String title;
final String description;
final String instruction;
final String why;
final String
recycle; //put either "recyclable" or "not recyclable" (This item "can be recycled")
final String
donate; //put either "can be donated" or "cannot be donated" (This item "can be donated")
Recyclable(this.title, this.description, this.instruction, this.why,
this.recycle, this.donate);
}
And here is the list:
List<Recyclable> recyclable = [
Recyclable('PAPERS', 'abc2', 'instruction123', 'why123', 'recyclable',
'cannot be donated'),
Recyclable('CLOTHING', 'abc3', 'instruction123', 'why123', 'recyclable',
'can be donated'),
Recyclable('CARDBOARDS', 'abc4', 'instruction123', 'why123',
'can be recycled', 'cannot be donated'),
Recyclable('COMPUTERS', 'abc4', 'instruction123', 'why123', 'recyclable',
'can be donated'),
];

One way you can iterate over the recyclable list is like this using forEach method
recyclable.forEach((item){
if(item.title == _outputs[0]["label"]){
//your code for when the match is found
}
});

Related

My condition doesnt work apprpraite in flutter for string

I have a list that stored by sqflite in flutter app, when i open the page i run this code in initState method to produce elements :
String _val_s_list=values_list_r.map((e) => e!=null ? e.s_redio_q : ' ').toString();
print("val_s_list: $_val_s_list"); printed: ///val_s_list: (1)
///method to return a string
choosen_radio_s(_val_s_list);
out of init state defined method as below:
String val_s;
String choosen_radio_s(_val_s_list){
setState(() {
if(_val_s_list=="1"){
return val_p1="1";
}
else{
return val_p1="2";
}
});
}
///print: val_s: 2
but i can not understand why this is not currect on first condition and always returns val_p1="2",
could some one help me about it??

How to Set Multple Regex Costraints on textbox in ZKOSS

I have a textbox which should only accept Characters:-for that first regex has been set in constraint and it should not accept some reserved keywords that are A,R,F,U .Since two different constraints are set ,i want user to see the specific message ,for first it should be Illegal Value i.e default zkoss error and when he/she enters a reserved character ,it should show that reserved code has been put.
But somehow the following code doesnt work :
field_code.setConstraint("/[a-zA-Z]/ : {Illegal Value} ,/[^AaRrUuFf]/ : Reserved Code");
The output is the first regex works fine but on offending the same " {Illegal Value} ,/[^AaRrUuFf]/ : Reserved Code" is displayed as error.
You can't do it in the zul, but with help of a SimpleConstraint you could create this.
Create your own class, and extend SimpleConstraint.
Then hold 2 Matcher vars for each constraint.
At last, override the Validate method to something like this :
#Override
public void validate(Component comp, Object value) {
if (value != null && value instanceof String) {
String stringValue = (String) value;
if (!expression1.reset(stringValue).matches()) {
throw new WrongValueException(comp, errorMsg1);
}
if (!expression2.reset(stringValue).matches()) {
throw new WrongValueException(comp,errorMsg2);
}
} else {
// do what needs to be done when value is null or not a String.
}
}

Index of string containing a part of string (one word)

im trying to read a large file, so i thought that instead of looping with an array i decided to use a list, but I'm having some difficulties with searching a line which contains a word that needs to be searched for. Here is my code
public List<string> AWfile = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(#"C:\DataFolder\file.txt"))
{
using (StreamReader r = new StreamReader(#"C:\DataFolder\file.txt"))
{
string line;
while ((line = r.ReadLine()) != null)
{
AWfile.Add(line); label1.Text = "ListWritten!"; label1.BackColor = Color.Green;
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int linen = AWfile.IndexOf("A102");
label2.Text = Convert.ToString(linen);
}
So my question is if there is any way to search just for a part of a word in a list instead of the whole string, because that's the only way the .IndexOf returns me anything at all.
You can try something like:
var result = list.Select(x => x.Contains("hello")).ToList()
This will result in a list with all the elements in the list which contains "hello".
And if you want to do something only with this elements:
list.Select(x => x.Contains("hello")).ToList().ForEach(x => DoSomething(x));
I hope this helps
If I understand your question correctly... you are reading in a file and adding each line to a list. Then you want to check if any of those lines contain part of a word.
One way of doing this would be to do a foreach loop over each of the lines in your list and checking if the line contains the partial word.
Something like:
foreach(var line in AWFile)
{
if(line.Contains("PartialWordWeWant"))
{
// Do something with the line that contains the word we are looking for
}
}

Groovy/Grails: Declaring a JsonBuilder inside a loop without overwriting previously generated jsons

Hello I am trying to create a list of json objects in groovy
List relClinicStatementList = []
for (BloodTestRow row in BTList){
def jsonListBuilder = new groovy.json.JsonBuilder()
def internalJson = jsonListBuilder{
'targetRelationshipToSource' {
'code' 'part-of'
'codeSystem' 'MG'
}
'observationResult'{
'observationFocus'{
'code' "${row.exam}"
'codeSystem' 'mobiguide'
'displayName' "${row.exam}"
}
'observationValue' {
'physicalQuantity' {
'value' "${row.value}"
'unit' "${row.unit}"
}
}
}
}
println jsonListBuilder.toPrettyString()
relClinicStatementList.add(internalJson)
}
And the toPrettyString() method correctly shows the json structure I want.
However if at the end of the loop I try to print all of the items I have in the list like this:
for (JsonBuilder entry in relClinicStatementList){
println entry.toPrettyString()
}
I get all the elements inside my relClinicalStatement list to be equal to the latest I created... I felt like declaring a new JsonBuilder at each loop would prevent this behaviour... am I missing something? I must admit I come from Java and have the feeling that using groovy classes here makes this behave a little differently from what I expect.
How do I solve this issue?
Thanks in advance
I can't reproduce the behaviour you are seeing, but I think the problem is that I don't believe internalJson is what you think it is (it's a list of 2 closures).
If you change your code to:
List relClinicStatementList = btList.collect { row ->
new groovy.json.JsonBuilder( {
targetRelationshipToSource {
code 'part-of'
codeSystem 'MG'
}
observationResult {
observationFocus {
code "$row.exam"
codeSystem 'mobiguide'
displayName "$row.exam"
}
observationValue {
physicalQuantity {
value "$row.value"
unit "$row.unit"
}
}
}
} )
}
relClinicStatementList.each { entry ->
println entry.toPrettyString()
}
Does it work as you'd expect?

Retrieve count of SelectList items?

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.