logic:iterate comparing the next value - struts

I am trying to perform a comparison to the current submission to the next. I have the following code:
<logic:iterate id="anItem" name="dataPage" property="filingsList"
indexId="myInd">
<bean:define id="currentSubmissionId" type="java.lang.String"
name="anItem" property="denseRank"/>
I would like to do something like:
<logic:equal name="dataPage" property="currentSubmissionId"
value="nextSubmissionId???">
<img id="plus<%=rowCount%>" class="cursor"
src="<icis2:icisRes/>images/plus.gif"
onclick="toggle('<%=rowCount%>', this,
jQuery('#minus<%=rowCount%>'))"/>
<img id="minus<%=rowCount%>" class="cursor"
src="<icis2:icisRes/>images/minus.gif" onclick="toggle('<%=rowCount%>',
this, jQuery('#plus<%=rowCount%>'))" style="display: none;"/>
</logic:equal>
Thanks,
Tom

1. Create a temp string variable to "",
2. Store the current String here
3. Iterate, and compare.
4. As you iterate check if equal or not.
5. If equal print else copy next string to the variable.
Here is the code.
String[] someArrayOfStrings; // Store the Strings here. You can use ArrayList too.
String tempString = ""; // Recommend to use ArrayList.
for(int count=0; count<someArrayOfStrings.length-1; count++){
tempString = someArrayOfStrings[count];
if(tempString.equalsIgnoreCase(someArrayOfStrings[count+1])){
System.out.print(tempString);
}
}
Let me know if it helped.

Related

Javascript with a for in loop

working on the follow code. Having some issues with taking the user input from the day and the temp. I have a start but again running into an issue with Step 2 & 3 unable to pass the information to the array and figure out how to display it. Any insight and direction would be greatly appreciated. Thanks
var temperatures = [];
var days = ["Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var $ = function (id) {
return document.getElementById(id);
}
var takeTemps = function () {
//###STEP 2
//Get the user inputted temp, validate it making sure it's a number
//if it's valid add it to the temperatures array at the index that
// corresponds with the day of week, e.g. 0 for Monday, 2 for Wednesday
var userTemp=(parseFloat($("tempIn").value));
while (!isNaN(tempIn)==true)
{
alert("Please enter a numeric value");
$("tempIn").focus();
}
//This gets the value from the selected menu option
var index = $("daySelect").value;
for(var dayTemp in temperatures)
{
var daily=temperatures[index]
}
//remove this when done, this just tests your menu you wrote for step 1
alert( index + " indexes day " + days[index]+ userTemp);
//Call displayTemps ONLY if the temp input was valid.
displayTemps();
//EXTRA work / not credit
// have it auto advance the selected day in the menu
// by assigning into $("daySelect").value
// If it was on Sunday change it to Monday and only on valid input
}
var displayTemps = function () {
//###STEP 3
//loop through non-undefined indexes in the temperatures array
//appended them to tempString adding the day .e.x
//Tuesday: 89
//Friday: 98
//display the string to the page by setting the value of the textarea
//
//In the same loop sum the temperatures and count
// how many there are so you can calculate the average
// and output the average temp on the page.
tempString = "";
tempTemp = 0;
for(var i in temperatures) {
tempString += index + ": " + temperatures[i];
}
document.write(tempString + "<br>");
var average =tempTemp+10;
$("tempList").value=tempString;
$("avgOut").value=average;
}
window.onload = function () {
$("addTemp").onclick = takeTemps;
//###STEP 1
//Use a for loop here to write options to the select for each day of the week
// <option value="0">Monday</option>
// using += here with innerHTML property takes the existing values and concats this on the end
for (var i =0; i<7; i++)
{
$("daySelect").innerHTML += "<option value=\""+ i + "\">" + days[i] + "</option>\n";
$("daySelect").value = "";
//var day=i-1;
//var day = days[i];
}
$("tempIn").focus();
}
Struggles with Step 2 and 3 Beleive i have #1 good to go I have enclosed the HTML for reference
<html>
<head>
<script src=script.js></script>
<head>
<body>
<section>
<select id="daySelect">
<option value="">Select a day</option>
</select>
<input type="text" id="tempIn">
<input type="button" id="addTemp" value="add temperature">
<br>
<br>
<label for="tempList">Temperature List</label>
<br>
<textarea id="tempList" rows="7" cols="50"></textarea>
<br>
<label>Average Temperature</label>
<input type="text" id="avgOut" disabled>
</section>
</body></html>
I would suggest to provide more specific information about which part of the code you are concerned with, and especially, provide the HTML code as well since that would enable us to see more clearly what you are trying to do.
When you are done, I will edit this answer and you will get appropriate guidance.
Keep coding!
EDIT
Take a look:
var userTemp=(parseFloat($("tempIn").value));
while (!isNaN(tempIn)==true)
{
alert("Please enter a numeric value");
$("tempIn").focus();
}
Looking at this little piece of code from "Step 2", I can think of a number of bugs already. Of course I'm not sure since I haven't seen your HTML yet, but it seems like:
You have put the value of the input into a variable named userTemp, yet you are checking for a variable named "tempIn" for validation. The second one probably doesn't exist at that point in time. "tempIn" was the name of your DOM element, not the JS variable you've assigned its value. You have to check for the userTemp variable.
In your validation, you are checking for the opposite of isNaN. NaN means "Not a Number", so the opposite of that would be actually number, so the statement is wrong. Not to mention that in this, you would not need to explicitly express "==true", you can check like this: while(isNaN(userTemp))
If you want to iterate a while statement for validation until you get a valid number, you need to put the variable assignment inside the while statement, since you'll need to try to assign the new number each time the validation loop iterates.
EDIT 2 - finished
Your code is live here:
https://codepen.io/bradib0y/pen/OBEdvp?editors=1010
Please note that if you are working your way through a course and this was your assessment, you've gained exactly nothing with me making these tasks for you. You will only gain from getting throught these challenges yourself.
I suggest to spend at least 1 hour with analyzing this code step by step and try to replicate it in a similar project. If you still have struggle with understanding, do yourself a favor and start over with basic javascript once again. You will be an expert on this within a week, if you put the basics down right. But if you still can't grasp the basics and keep pushing forward with more complex issues, you will have a hard time.

How to setText in a textArea from an ArrayList?

I have an ArrayList ArrayList<String> externalDataList = new ArrayList<>(1600);and I would like to display in a textArea first 3 strings, but I can't succed:
Here is my code
textareaShowPreview.setPrefRowCount(3);
Iterator<String> it = externalDataList.iterator();
int tot = 0;
while(it.hasNext() && tot<3){
String element = it.next();
textareaShowPreview.setText(element + "\n");
System.out.println("elements are: " + element);
tot++;
}
The sout correctly print first 3 strings
element are: 23/05/2007 ,30.9455,31.2545,30.9091,30.9545,7518142
element are: 24/05/2007 ,30.6545,31.0909,30.5364,30.6909,12851606
element are: 25/05/2007 ,30.6636,30.8545,30.4818,30.8091,9392088
but in textArea I have only first one
How do I have to modify my code to show in textArea all three strings, one string per row?
Use appendText instead of setText here is a link.
The setText, delete the previous text and set the text you are giving to it. The append keep the current text in your text area.
Hope it helps!

Count number of elements that has a score

Is there a way to count the number of elements that isn't equal to 0.00?
For example, the code is
<div id="average_2123" style="font-size:20px; ">0.00</div>
<div id="average_2124" style="font-size:20px; ">23.53</div>
<div id="average_2125" style="font-size:20px; ">0.00</div>
How can I count the element so it's only 1 since only one of them has a score?
I want to do this on PHPUnit. I can also do it on Selenium IDE because I can convert it to PHPUnit
you will have to write custom code. I am writing java pseudocode. hope you can understand and convert
List<WebElements> ElemList = Webdriver.FindElements(By.Xpath("//div")
for (i = 0; i < ElemList.size();i++)
{
WebElement Current =List.getElementAt(i);
String ElemName = current.getAttribute("id");
String text =""
int Count = 0;
if( id.Contains("average"))
{
if( !id.getAttribute("value").equals("0.00")
{
count++;
}
}
}
A better approach can be as mentioned below. Im writing the code in Java:
List<WebElement> elemList = driver.findElements(By.cssSelector("div[id^='average']"));
List<WebElement> filteredElements = new ArrayList<WebElement>();
for (WebElement element : elemList) {
if (Long.parseLong(element.getText()) > 0.00)
filteredElements.add(element);
}
This will be find all the elements whose "id" attribute starts with "average".
Also here i am converting the text to long and then comparing whether its grater than 0.00
The filteredEleemnts are the elements which have value greater than 0.00

Is there a built-in function to extract all characters in a string up until the first occurrence of a space?

Is there a built-in function to extract all characters in a string up until the first occurrence of a space?
Say the string is:
Methicillin-resistant staphylococcus aureus
I want to be able to get the substring:
Methicillin-resistant
You can do it in two functions:
newstring = mystring.Substring(0, mystring.IndexOf(" "))
Although that will fail if there's no space in mystring.
So you could pull out mystring.IndexOf(" ") into a variable and check whether it's -1 (no space found) before you try to use it in Substring.
The first solution you can use is a simple IndexOf
string GetFirstWord(string source)
{
int index = source.IndexOf(" ");
if (index == -1) return source;
else return source.Substring(0, index);
}
The second solution can be used if you want to keep all words into a string array.
string[] GetWords(string source)
{
return source.Split(' ');
}
if you only want the first word, you can use it like this :
string word = GetWords("Methicillin-resistant staphylococcus aureus")[0];
And a VB.NET solution. No, it can't be done with one built-in method; you need two:
Left(myString, InStr(myString, " ") - 1)
And like the other solutions you need to check InStr doesn't return 0 if myString may not contain a space.

How to use a dynamic smarty variable in foreach loop

Can anyone tell me how to use dynamic variables in smarty foreach loop. I am trying to create a module in prestashop and m very close to get it done.
here's my code:
//file name index.php
foreach( $subCategories as $s )
{
$foo = intval($s['id_category']);
$k = new Category($foo);
$var1 = "subSubCategories.$foo";
$var1 = $k->getSubCategories(1);
$smarty->assign(array('foo'.$foo => $var1));
}
//file name:index.tpl
{assign var=foo value=$foo$cat}
//where $cat is a variable that counts the number of categories
{if isset($foo) AND $foo}
{foreach from=$foo item=subCategories name=homesubCategories}
<p>{$subCategories.name}</p>
{/foreach}
{else}
<p>{l s='test failed'}</p>
{/if}
I've exhausted all of my resources and knowledge and feeling quite helpless at this moment. so plz help me out.
I am trying to create dynamic variables ('foo'.$foo i.e. foo1, foo2, and so on) depending on the number of sub-categories. I think m successful up to this point. Now moving on to the tpl file, here I want to access the dynamically created variable (foo2, foo3 etc.) using foreach. Now if I am doing this: {assign var=foo value=$foo3} I succeed in fetching the values from this subcategory using the same foreach loop. But when I do this: {assign var=foo value=$foo$catx} (where $catx stores the values for category id) it fails. Please help.
I use such code in section loop (for Smarty 2):
{section name=buildings loop=7 start=1 step=1 max=7}
<tr>
{assign var=part_number value="part`$smarty.section.buildings.index`_number"}
<td class="number">
<input class="number xshort" name="part{$smarty.section.buildings.index}_number" value="{$data.$part_number}" type="text">
</td>
</tr>
{/section}
As you can see, there are a creation of dynamic variable part_number (see backticks in value block). And then, you can use it as value="{$data.$part_number}".
Your $smarty->assign() call is incorrect; you've only got one parameter being sent to it, but it needs two parameters (the variable name, followed by the value). Based on your template code (looping through variable $foo), your loop should be something like this in PHP:
//file name index.php
$subcats = array();
foreach($subCategories as $s) {
$foo = intval($s['id_category']);
$k = new Category($foo);
$subcats[$foo] => $k->getSubCategories(1);
}
$smarty->assign('foo', $subcats);
Then it also looks like you're not using the Smarty {foreach} quite properly either. You're declaring the name attribute, of the foreach loop, but then not using it properly. Assuming $k->getSubCategories(1) returns a string, the smarty code would just have to be:
{foreach from=$foo item=subCategories}<p>{$subCategories}</p>{/foreach}
If $k->getSubCategories(1) returns an array with a 'homesubCategories' element (which is what I think you're trying to get at with your Smarty code), you still don't need the 'name' value on the foreach. You'd then just do:
{foreach from=$foo item=subCategories}<p>{$subCategories.homesubCategories}</p>{/foreach}
With $foo = intval($s['id_category']); you defined $foo as an integer.
Then you concat the string with $smarty->assign(array('foo'.**$foo** => $var1));
Correct is:
$foo = $s['id_category'];
$smarty->assign(array('foo'.$foo => $var1));
Dynamically named variables are generally a very bad design choice, making everything much more difficult than it needs to be.
If you have a set of elements you need to loop over or index into, put them in an array; then you don't need to do any magic assignments, as you can use normal foreach loops or array key access:
//file name index.php
// Build the array first, then assign it to Smarty at the end
$foo_array = array();
foreach($subCategories as $s)
{
$foo = intval($s['id_category']);
$k = new Category($foo);
// This line is redundant as you over-write on the following line:
// $var1 = "subSubCategories.$foo";
$var1 = $k->getSubCategories(1);
$foo_array[ $foo ] = $var1;
}
// Assigning one variable, so just pass name and value to $smarty->assign()
$smarty->assign('foo_array', $foo_array);
//file name:index.tpl
{* Look for the entry in the outer array with key "$cat", then loop over it *}
{foreach from=$foo_array[$cat] item=subCategories}
<p>{$subCategories.name}</p>
{/foreach}
it's possible to call variable dynamically created in template with this statement:
if for example $index = 2
{$foo{$index}}
then this will call variable with name {$foo2}