I'm only learning cucumber.Tell me if it's possible to get something from '(.*)' into a variable?
#Then("^ Page is '(.*)' on PDP$")
For example : #Then("^ Page is 'display' on PDP$").And I want to get the word 'display' in a variable. Because i want work with it in my method.
I want something like this
#Then("^ Page is '(.*)' on PDP$")
public void title() {
String str = '(.*)';
...
}
When you use a cucumber and write some description in a step, you can select the desired word and receive it in the method arguments.
#Then("^ Page is '(.*)' on PDP$")
public void title(String str) {
String str = '(.*)';
...
}
Related
I've been trying to declare multiple strings as parameters to a method without specifying their type for every parameter, however I've not been successful.
What I've got:
private void Fields(String sName, String sSurname, String sMessage) {
teName.setText(sName);
teSurname.setText(sSurname);
teMessage.setText(sMessage);
}
What I want:
private void Fields(String sName, sSurname, sMessage) {
teName.setText(sName);
teSurname.setText(sSurname);
teMessage.setText(sMessage);
}
Of course the second example doesn't work, but it's just to give you an idea of what I mean to do.
Is there a shorter way of doing this?
You have surely tried to run your code and got a syntax-error.
No - it is not possible that way.
But you can do the following:
private void Fields(String[] mydata) {
teName.setText(mydata[0]);
teSurname.setText(mydata[1]);
teMessage.setText(mydata[2]);
}
The call could be:
Fields(new String[] { "whatever", "strings", "you want" });
That way you have to take care of your index and risk an arrayindexoutofbound-exception. But it is possible.
Forgive me for the beginner question. I'm trying to build a web scraper . I made a helper class Selectors to store CSS selectors of different type and the class has a getter method that returns a optional field value:
public Optional<String> getName() {
return Optional.ofNullable(this.name);
}
In my main Scraper class I have a method to extract the name from the HTML using the Selectors object like this:
public String extractName(Element building) {
if (this.selectors.getName().isPresent()) {
String cssSelector = this.selectors.getName().get();
Elements buildingNameElement = building.select(cssSelector);
return buildingNameElement.text();
}
return "N/A";
}
As much as I've read this isn't a very nice way to use the Optional class. But I'm struggling to come up with a better solution. My first thought was to use the ifPresent method but it does not work in this manner:
public String extractName(Element building) {
this.selectors.getName().ifPresent(() -> {
String cssSelector = this.selectors.getName().get();
Elements buildingNameElement = building.select(cssSelector);
return buildingNameElement.text();
});
return "N/A";
}
I'd like that the Elements.select() would execute only if there's a name field present in the Selectors object. Could anyone help me make the code a bit more functional?
Thanks!
public String extractName(Element building) {
return this.selectors
.getName()
.map(cssSelector -> {
Elements buildingNameElement = building.select(cssSelector);
return buildingNameElement.text();
})
.orElse("N/A");
}
This is what Optional.map is for. When you do return inside a lambda, you are only returning from the lambda, not from the outer method. So the above uses the text of the building name element if getName returned a name/selector. And returns N/A if not.
If you’re fine with a more condensed syntax and fewer named variables, you may go with the following:
return this.selectors
.getName()
.map(cssSelector -> building.select(cssSelector).text())
.orElse("N/A");
Disclaimer: I haven’t got JSoup on my computer, so I haven’t tested. Please forgive if there’s a typo, and report back.
I want to implement a pakistan's standard format of cnic number which is like this:12345-1234567-1.
But I don't know anything about this. I found the following code for this purpose but it also giving errors in NetBeans.
private void idSearchKeyPressed(java.awt.event.KeyEvent evt) {
String cnicValidator = idSearch.getText();
if (cnicValidator.matches("^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$")) {
idSearch.setEditable(true);
}
else {
idSearch.setEditable(false);
}
}
The pattern is correct. But it can be condensed to this:
^[\\d]{5}-[\\d]{7}-\\d$
Where does idSearch come from? If its not a final member of the class you can't access it in that way. So make sure idSearch is available inside idSearchKeyPressed. Also make sure that there are no trailing spaces or something like that. You can do this by calling
cnicValidator = cnicValidator.trim();
The following example returns true for both regex versions.
public static void main(String... args){
String id = "35241-7236284-4";
System.out.println(id.matches("^[\\d]{5}-[\\d]{7}-\\d$"));
System.out.println(id.matches("^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$"));
}
I have a map that I am referencing within my JFrame in a JList/ScrollPane object so it displays like this:
tongs ... 4.99
oven mitt ... 9.99
and so on. I want to use the users selection to populate a textbox in another form. I have this working just fine, however it currently displays the whole string with the ... [price]. I'd like to only display the object.
I've thought about using the .split method, but I'm not sure if that's correct. Any ideas to help me out here?
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(view.getSubmit())) {
dialog.setObject(view.getSelection()); // This prints the whole string as shown above
dialog.setVisible(true);
}
...
}
setObject() goes to a method tied to a textbox. Instead of displaying 'oven mitt ... 9.99' I'd like it to only say 'oven mitt'. The hard part is that the number of words vary, so I cannot just say take substring xyz.
EDIT: I got it using the following code.
if(e.getSource().equals(view.getSubmit())) { // Launch kiosk
String search, val = null;
search = view.getSelection();
for (myEnum d : myEnum.values()) {
if (search.contains(d.getLocation())) {
val = d.getLocation();
}
}
dialog.setDestination(val);
dialog.setVisible(true);
}
For example I have Hello123456 string and I want to show it as just Hello in a TextView.
I need those numbers in my code and I can't erase them.
EDIT:
All answer are providing a way that I erase the numbers and then set it to textview, But I want them to be there.
I get the "hello" part from user and I add the number part to it and this will be the name of a listview Item. but I want to show just the hello part in listview and also if I checked if that listview clicked item ends with "123456" it returns true.
I want to show it as just "Hello" in a TextView.
Then just put Hello in your TextView without cutting your string or create an temporary string to hold the "Hello" String.
If your string is like this "HELLOHI12345" then you need a regex to eliminate all the number string within it.
sample:
textview.setText(s.replaceAll("[0-9]+", ""));
Also take note that string are immutable so the original String wont get replaced after executing replaceAll
Use this dude ! :) You can use contextDescription to access the actual text
textView.setText(yourText.replaceAll("[0-9]",""));
textView.setContentDescription(yourText);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str = ((TextView) v.getContentDescription()).toString();
}
});
A TextView can have a tag attached to it i.e. some object that is stored as extra data to the view itself. Check out the documentation here:
View.setTag(Object)
In your case, you can assign the "Hello" to the displayed text (using the method below), and set the tag to the full text.
textView.setText(getDisplayText(helloString));
textView.setTag("TAG", helloString);
When the user clicks on your view, you can get the tag from the view and do what you need with it.
This way, there is more data stored than what you see.
(original answer before the edit)
How about a method that does what you need:
private String getDisplayText(String input)
{
return input.substring(0, 5);
}
And then just use that method when you set the value of your TextView.
textView.setText(getDisplayText(helloString));
use \\w+ regex with Pattern and Matcher classes. This will also work if you don't know the String length. The String can be hello123 or olo123
Its not possible to hide the characters in a string. You have to solve your problem in a different way. My suggestion is to implement a separate class which contains the string:
I would write an class which contains the string. Then add two methods to this class which returns the string for display and for internal use. E.g.
class DataObject {
private String data;
public String forTextView() {
//code from one of the answers above, may be the regex version
}
public Stringg getStringWithNumbers() {
return data;
}
}
This allows you to show the string without the numbers and still have them ready when you use them.