How to choose radio button. `//choose user gender
String male = "male" ;
String female = "female";
String other = "other";
System.out.println("User gender");
gender = sc.nextLine();
System.out.println(gender);
if (gender.equalsIgnoreCase(female)) {
driver.findElement(By.cssSelector("#gender_female")).click();
}
else if (gender.equalsIgnoreCase(male)) {
driver.findElement(By.cssSelector("#gender_male")).click();
}
else if (gender.equalsIgnoreCase(other)) {
driver.findElement(By.cssSelector("#gender_other")).click();
}`
by the code above I want to select the radio button according to the user choose: my problem is that when user chooses either female or male or other it will not work. the program will be terminated. How to choose radio button by user input. Please help
Look to lower left frame of your Eclipse window: There you've got an ElementNotFoundException. This means that your locator (in this case By.cssSelector("#gender_male")) does not resolve to an HTML element.
Change the locator to some valid expression (depending on your web page code) and it should work.
Related
Hello fellow StackOverflowers, I hope all of your days are going well.
I'm relatively new to Java programming and have found myself in a bit of pickle.
What I'm attempting to do is;
Input Validation in Java - I want to make sure that the JOptionPane.showInput pane continues to re-appear (using a while loop) until the user has entered a value which is captured in the "this.accountName" String and;
From there once the user has entered something in the JOptionPane.showInput pane I want to exit the loop and proceed to the other methods I have inside my OO program.
Unfortunately my while loop below exits after the first instance and doesn't continue in my code example below;
public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}
What would be the best way to go about fixing this?
I appreciate your help in advance!
Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
while (StringUtils.isBlank(this.accountName)) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
}
return this.accountName;
I am developing a custom keyboard.In this, I use an Enter button which works well as an enter key when I typed in a text or messaging But I want to use this button as a search key when any one type in a browser. In my keyboard, this works as newline instead of search.How can I handle this with the same button?
I am following this Link to make this Keyboard
https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615
here is code
if(primaryCode == Keyboard.KEYCODE_DONE ) {
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
}
Maybe you could use:
if(primaryCode == Keyboard.KEYCODE_DONE ) {
if(Patterns.WEB_URL.matcher(editText.getText().toString()).matches()){
search();
} else {
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
}
}
I have a swing application am working on, my application lets the user enter a first name, last name, and phone number. the the user clicks the add button and it adds the entries into the jlist(so it like a phone book). I have a jTextfield above the JList in which I want to allow the user to search for a specific name or phone number on the Jlist, so its kind like a google search you type a character and it shows names with relevant characters in the JList and such. Am really stuck and lost at this point any help would be appricated??
This is my add button code to add names to my Jlist:
private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {
String firstName = txtFirstName.getText();
String lastName = txtLastName.getText();
String phoneNum = (txtPhoneNum.getText());
NumberFormat number = NumberFormat.getNumberInstance();
//Phone Number formatted
StringBuilder sb = new StringBuilder(phoneNum).insert(0, "(")
.insert(4,")").insert(8,"-");
String phoneNumFormatted = sb.toString();
contactsArrayList.add(firstName + "\t " + lastName + "\t " + phoneNumFormatted);
DefaultListModel<String> model = new DefaultListModel<>();
for(int i = 0; i < contactsArrayList.size(); i++)
{
String myArraylst = contactsArrayList.get(i);
model.addElement(myArraylst + "\t");
}
listPhoneBookContacts.setModel(model);
txtFirstName.setText("");
txtLastName.setText("");
txtPhoneNum.setText("");
}
It is possible to implement this kind of stuff in Swing, but it is gnarly and you are unlikely to do a good job of it (because it's hard). You're probably better off leaving it to some other library, like SwingX. They have a bunch of components you can use that might do exactly what you want.
If you don't want to use that, a quick Google search reveals a good tutorial for filtering JLists.
my application lets the user enter a first name, last name, and phone number
I would use a JTable to display all this information.
so its kind like a google search you type a character and it shows names with relevant characters
JTable has built in filtering. See Sorting and Filtering for a working example
I've googled the holy bejaysus out of it and just can't find the right combination of words. How do I use methods on something with no identifier? For instance;
String inputBio = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);
How do I refer to that dialog?
So you want to assign the value typed into the input box to the String? You need to create an Integer and assign it to the JOptionPane.showInputDialog, then use the constructor to instantiate a new instance of the Input Dialog with the options you want.
Now, to extract the data, add an IF statement and go through the possible options presented to the user. If you added JoptionPane.OK_CANCEL_OPTION then you have two options: OK and CANCEL.
The IF statement should be: If the user selected OK, extract the string, ELSE do whatever you want to do.
The string should be extracted from the textfield that was created (which I assume is somewhere in the 'heroPane')
Here's how that would look in code
//the string we use for input
String inputBio;
int optionBox = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);
//now the integer value represents the option selected (OK or CANCEL)
//All we need to do is extract the data and assign it to the string
if (optionBox == JOptionPane.OK_OPTION) {
//the OK option
inputBio = nameOfYourTextField.getText();
} else {
// we have a CANCEL option
}
You could, of course, do it without an IF statement, but make sure you handle all of the options accordingly
If you need a reference to the JOptionPane, you cannot use the showInputDialog convenience methods. You will need to construct a JOptionPane object.
I am working on some project, where a user can fill in student details in a form with textfields and radio buttons etc, and the values shall then be used to create a record in a database.
Now, this is how i started:
String firstName = textField_1.getText();
String surname = textField_2.getText();
String Gender = rdbtnM.getText();
Basically, what it does is, its retrieving values from particular input fields.
Now, my question is, for gender i have two radio buttons. rdbtnM and rdbtnF.
A user can only click on one: Male or Female.
How do i write in String Gender, that it should get the value from M or F ?
Please advise?
I suppose you are using Swing, with two JRadioButton objects inside the same group, but your question is very imprecise. If you want to do it in a simple way, you can do the following:
String gender // make your variable names start lower-case
if (rdbtnM.isSelected()) {
gender = "male";
}
else if (rdbtnF.isSelected()) {
gender = "female";
}
else {
gender = "unknown";
}
You can of course avoid the last case by initializing the button states properly, but you don't provide any initialization code. There is an official tutorial about buttons, which also explains how to deal with radio buttons by the means of ActionListeners, which I avoided here.