I would like to compare the string in a textbox if it contains "per/kg" and use that to disable a button. I have tried several methods but it did not work please kindly help out.
if (productDescTextBox.getText().equals("per/kg"))
{
buttonDot.setEnabled(true);
}
else buttonDot.setEnabled(false);
and this
if ("per/kg".equals(productDescTextBox.getText().toString()))
{
buttonDot.setEnabled(true);
}
else buttonDot.setEnabled(false);
use String.contains(CharSequence)
if (productDescTextBox.getText().contains("per/kg"))
Use string.contains.
if (productDescTextBox.getText().contains("per/kg")){
//Whatever you want
}
Note the method is case sensitive
Related
SO i have this application that uses retrofit client to send a request to the ROS server and now my problem is that I am setting up my Status that if the status is "True" it will set to textcolor as GREEN and else as RED but when i execute it to my application it seems that its only executing the else statement it displays all red even if the status is true. Can anyone help me with this Im just a beginner.
HardwareStatus is a TextView, so it's always not equal to false. Maybe you want to check it text? In that case you should use TextView.getText() method
if (HardwareStatus.getText().equals("false")) {
HardwareStatus.setTextColor(Color.RED);
} else {
HardwareStatus.setTextColor(Color.GREEN);
}
And the first letter of variable should be lower case HardwareStatus => hardwareStatus
Duy Khan Ngyuen answer is right, I just suggest you to use equalsIgnoreCase.
if(HardwareStatus.getText().toString().equalsIgnoreCase("false")){
HardwareStatus.setTextColor(Color.RED);
}else{
HardwareStatus.setTextColor(Color.GREEN);
}
It seems like you haven't get the text of the SoftwareStatus , you need to get the text first convert it to string and then compare it if it is true .
if(SoftwareStatus.getText().toString().equals("true"))
{
SoftwareStatus.setTextColor(Color.GREEN);
}
else
{
SoftwareStatus.setTextColor(Color.RED);
}
It is because .equals() is usually used for comparing strings/characters/text
edited.
after getting your response update your Boolean or string value in app class create setter and getter for that then simply on before add check on your textView `
Class ApplicationClass extends Application(){
String statusValue="false";
Boolean statusBoolean=false;
public void updateStatusValue(String value){
this.statusValue=vale;
}
public String getStatusValue(){
return statusValue;
}
}
after calling API and getting response updateStatusValue
ApplicationClass.updateStatusValue(response.body().getHardwareStatus())
if(ApplicationClass.getStatusBoolean() ||Application.getStatusValue().equalsIgnoreCase("true")) {
SoftwareStatusTv.setTextColor(Color.GREEN);
}else{
HardwareStatusTv.setTextColor(Color.RED);
}
now you can add check for whole application using this simple getter setter
I'm trying to validate/filter my jtextbox wwith this Regex:
^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$
I want to filter two names with one space.
Tried using keytyped and keyreleased, but it just does not work (won't let me write anything on the textbox) and e.consume() does not work.
boolean StrCheck(String Exp,String str) {
return Pattern.matches(Exp,str);
}
#Override
public void keyTyped(KeyEvent e) {
if (e.getSource() == jTextField) {
String regexp = "^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$";
if (jTextField.getText().length() == 25) {
e.consume();
} if (StrCheck(regexp,jTextField.getText())){
}else {
e.consume();
}
I've been searching, but the only possible answer I got, was to create a Documentlistene BUT can't find any example or how actually do it and make it work.
Neither KeylListener, neither DocumentListener will work. In most of this kind of cases you would need to use a DocumentFilter. Before you do though, take a look on how to use formatted textfields. It might be enough for you. If it does not, this answer is what you are looking for.
I have this logic in my code
if(r40.isChecked()){
//do this
}
It works great, but I needed the negative, "if rb.isNotChecked" or ".isChecked() = false"
But I cant see to find it, also I can't use the RadioGroup because all my RadioButtons are not inside one, I'm controlling them manually
But I cant see to find it
if(!r40.isChecked()){
//do this
}
I mean you COULD do if(r40.isChecked() == false){
//do something
}
BUT if(!r40.isChecked){
//do something
} Is much better/cleaner
Try with negative check
if(!r40.isChecked()){
//do this
}
this check if radio isn`t checked.
Try this:
if (! r40.isChecked()) {
//Do something
}
In more detail the exclamation mark added in this code reverses the following code in the same block, causing the code to mean:
if (r40.isNotChecked()) {
//Do something
}
I hope this helped!
BTW, this is only a short version of my code, the only problem I have is from .equalsIgnoreCase over. I have tried the pipe operator || and that has not worked for "or" either. Let me know, thanks. Its in Java too.
if(sWord.substring(0,sWord.length()).equalsIgnoreCase("ch","sh","s","x","z"
{
lblPluralOutput.setText(sWord + "es");
}
}
No, you cannot do it directly like that. Put all possible values in an array and check your string is in that array or not.
String[] items ={"ch","sh","s","x","z"};
for (String item : items) {
if (sWord.substring(0,sWord.length()).equalsIgnoreCase(item)) {
lblPluralOutput.setText(sWord + "es");
break;
}
}
More over sWord.substring(0,sWord.length()) again gives you same string back. Is it a typo ?
Those functions only take one parameter.
If you want to check whether a string is equal to either of two things, you need to check separately:
if (a.equals(b) || a.equals(c))
You can't use String#equalsIgnoreCase(Str) cause only receives one parameter. But you can
make your util method.
Something like this. We can make it generic.
public final class UtilClass{
private UtilClass(){}
public static <T> boolean isSomeOneEquals(T myParam, T ... a){
return Arrays.asList(a).contains(myParam);
}
}
So in your example just put:
UtilClass.isSomeOneEquals( sWord.substring(someIndex,sWord.length()).toLowerCase(), "ch","sh","s","x","z" );
I have this block of code that is giving me results for a combo box, I would like it to ignore the combo box and disable it when the value "SDO/OD" is selected in the one above under the combo box for ROLE aka fcbRole. The following enables the box from the first part, but the second part does not fire off. And it gives me a warning: "This field is required"...Have you seen something like this before?
I have been tinkering with:
fcbRole.addSelectionChangedListener(new SelectionChangedListener<ModelData>()
{
#Override
public void selectionChanged(SelectionChangedEvent<ModelData> se)
{
if ("SDO/OD".equals(this.toString()))
{
fcbOfficeRegion.enable();
} else
{
fcbOfficeRegion.disable();
}
}
});
Don't use == and != to compare Strings, instead use:
if("SDO/OD".equals(this.getStringName()) // or make sure you override toString()
// enable
else
// disable
For String value equality, use equals() method and not operators. Operators does reference equality check.
So, change your code to:
if ("SDO/OD".equals(this.toString()))
{
fcbOfficeRegion.enable();
} else
{
fcbOfficeRegion.disable();
}