How to check if there is text within my JButton - java

I want to check whether there is something in my JButton. What would i insert into the equalsIgnoreCase() area?
if (jButton1.getText().equalsIgnoreCase("") &&
jButton2.getText().equalsIgnoreCase("") &&
jButton3.getText().equalsIgnoreCase("") &&
jButton4.getText().equalsIgnoreCase("") &&
jButton5.getText().equalsIgnoreCase("") &&
jButton6.getText().equalsIgnoreCase("") &&
jButton7.getText().equalsIgnoreCase("") &&
jButton8.getText().equalsIgnoreCase("") &&
jButton9.getText().equalsIgnoreCase(""))

To check that there is a text, you can do:
!jButton1.getText().isEmpty()
or, if you want to exclude a text that only contains spaces:
!jButton1.getText().trim().isEmpty()

You can use
jButton1.getText().isEmpty()
(use ! to negate if you want to know it's not empty...)
or you can check the length of the value
jButton1.getText().length > 0

you can use
bool somethingIn = !jButton1.getText().isEmpty();

Nothing, just what you did. But you should put exclamation symbols before conditions to negate them:
if (!jButton1.getText().equals("") &&
!jButton2.getText().equals("") &&
!jButton3.getText().equals("") &&
!jButton4.getText().equals("") &&
!jButton5.getText().equals("") &&
!jButton6.getText().equals("") &&
!jButton7.getText().equals("") &&
!jButton8.getText().equals("") &&
!jButton9.getText().equals(""))
In this case body of if will be executed if all your JButtons have some text. And ignoreCase is not necessary. Emptiness has no lower or upper case ))

Related

Confused on how to use || and && in the same if statement. Can anyone spot the problem?

I am confused on how to use || and && in the same if statement.
What I am trying to do is have it print something if the string starts with an "D" or an "O", and if one is true check if the string has a character length of two.
Example: if the string is "DE" it will print something. However, if it is "SE" it will not print anything.
else if( (answer.startsWith("D") || answer.startsWith("O"))
&& (answer.length() == 2) ) {
//print something
}
Java is applying a "short circuit" to your logic. It does the first part (starts with "D" or "O") and if that's true it proceeds to the second part (length is 2). However if the first part evaluates to false then it never even bothers to execute the second part.
So your "SE" string will never go into the "print something" bit because it doesn't meet your first criteria of starting with D or O. And based on your description of what the logic should be, that is correct.
If you actually mean that if it starts with "D" or "O" OR is 2 characters long then your logic statement should have been:
else if( answer.startsWith("D") || answer.startsWith("O")
|| (answer.length() == 2 ) {
//print something
}
Edit: Oops, just pasted the original code in the first time...!
I would check first the length and after the two conditions e.g.
else if (answer.lenght()==2) {
if (answer.startsWith("D") || answer.startsWith("O"){
//print something that lenght is 2 and starts with D or O
}
}
}
In that case you have to check length first because && will true if left side and right side both true
else if( (answer.length() == 2)&&(answer.startsWith("D") || answer.startsWith("O"))
{
//your logic
}

Java && operators

I'm trying to make simple if statement , I need to see if my two String's values are not empty (does not equal "").
I use && operator , but sadly , it only checks one string properly if it's not empty , and if the second string is empty, he passes . Making && kinda useless for me.
if ( StringUtils.isNullOrEmpty(name) && StringUtils.isNullOrEmpty(sname)
) {do something}
I need them both to be checked properly . If at least one string is empty return false .
If both are not empty return true.
Currently your condition is true if both Strings are null or empty.
If you want both not to be null or empty you need :
if ( !StringUtils.isNullOrEmpty(name) && !StringUtils.isNullOrEmpty(sname)
) {do something}
&& is smart and if first condition is false it doesn't try the second.
use & could achieve what you want.
The problem is that your check is wrong. You want it to only pass if both ARE empty.
So you should go for
if ( !StringUtils.isNullOrEmpty(name) && !StringUtils.isNullOrEmpty(sname)
) {do something}
or if u want to catch it in the if statement if one of them IS empty, you should use or:
if ( StringUtils.isNullOrEmpty(name)|| StringUtils.isNullOrEmpty(sname)
) {do something}
isNullOrEmpty will return true if name is empty, but you want it NOT empty, so negate the conditions using !.
if ( !StringUtils.isNullOrEmpty(name) && !StringUtils.isNullOrEmpty(sname)
) {do something}
Another alternative is:
if (!(StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(sname)
)) {do something}
However, the above is not a recommended coding style, as it is difficult to catch the ! hidden just before the line, and code readers may get confused.
About || and && operators optimized checking:
|| checks for the first true condition from left to right and skips any further checking if it finds one true condition.
&& checks for the first false condition from left to right and skips any further checking if it finds one false condition.
At first it didn't looked any sense to me , and it didn't worked . Only then I realized that I need to to flip the return statements.
if (!StringUtils.isNullOrEmpty(name) && !StringUtils.isNullOrEmpty(sname)) {
System.out.println("ok");
return true;}
else {
System.out.println("empty names");
return false;
}
Now it seems to work just fine.
Using '&' as logical operator you can do the following thing. Here we have not use StringUtils -
if( ( null!=name && !name.isEmpty() ) &
( null!=sname && !sname.isEmpty() ) ){do something}

Check if its a character

After getting an action from the KeyListener, using the event.getKeyCode() and later on the KeyEvent.getKeyText(keyCode), how would i check if the outcome of .getKeyText(keyCode) is a single character like 'a' and not a whole word like "Space" ?
How about this:
KeyEvent.getKeyText(keyCode).length == 1
You can use getKeyChar() of the KeyEvent, and then'll you be certain that what you get back is a single char.
E.g. something like this:
public void keyTyped(KeyEvent e) {
keyChar = e.getKeyChar();
...
}
Try this:-
if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
{
if(KeyEvent.getKeyText(keyCode).length == 1)
{
//Only one character is pressed.
}
}
This solution worked for everything except the delete key and numpad keys...
event.getKeyChar() != '\uFFFF'
Because any Java outputs that character for non-renderable keys, it works quite consistently.
To fix the delete key problem...
event.getKeyChar() != '\uFFFF' && event.getKeyCode() != KeyEvent.VK_DELETE
It will return true if the key is printable, and false if not.

how to validate the null string array (Android Java)

I have a question about how to check either the array string got the null value.
My code is like below but still got the string return even the value is null.
for (int i=17;i<29;i++)
{
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
{
Log.d("Get additional label","Additional label = "+label[i]);
}
}
Problem Solved
The problem solved when I change from
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
to
if (label[i].length() != 0)
Thanks for those who replied :)
You could try using the StringUtils api from the apache commons lib:
if(!StringUtils.isEmpty(label[i]))
Check out the docs for more details.
Use == or != to check for null.
e.g. label[i] != null and also the way have it now label[i].equals should end up in NPE if it's really null.
Are you sure your string is actually null and not just an empty string?

Enforcing valid input in Java

I have a class I wrote in Java and one of the methods is getCommand()
The purpose of this method is to read in a string and see what the user typed in matches any of the acceptable commands.
This is how I wrote it initially:
public char getCommand(){
System.out.println("Input command: ");
command = input.nextLine();
while(command.length() != 1){
System.out.println("Please re-enter input as one character: ");
command = input.nextLine();
}
while( command.substring(0) != "e" ||
command.substring(0) != "c" ||
command.substring(0) != "s" ||
command.substring(0) != "r" ||
command.substring(0) != "l" ||
command.substring(0) != "u" ||
command.substring(0) != "d" ||
command.substring(0) != "k" ||
command.substring(0) != "f" ||
command.substring(0) != "t" ||
command.substring(0) != "p" ||
command.substring(0) != "m" ||
command.substring(0) != "q"){
System.out.println("Please enter a valid character: ");
command = input.nextLine();
}
fCommand = command.charAt(0);
return fCommand;
}
Now, I see the problem with this is that since I use the OR operator, it won't escape that loop because the character I type in will always not equal one of them. I tried changing it to the AND operator, but same problem. What would be the best way to only accept those specific characters?
Much appreciated.
Your logic is incorrect. You should be using logical ANDs and not ORs. Also I believe you want to use charAt() instead of substring() then compare characters.
i.e.,
while( command.charAt(0) != 'e' &&
command.charAt(0) != 'c' &&
command.charAt(0) != 's' &&
...)
Otherwise if you want to test for actual single-character string inputs, just check using string equality.
while( !command.equals("e") &&
!command.equals("c") &&
!command.equals("s") &&
...)
You should define your commands as constants (individually). Hard coding values like this makes it harder to update your code in the future.
If the program is simply proof of concept or homework I would use:
private static final String COMMANDS = "ecsrludkftpmq";
while(!COMMANDS.contains(command.getChar(0)) {
System.out.println("Please enter a valid character: ");
command = input.nextLine();
}
Otherwise, if this is production code I would consider making a simple Command(char) class and providing individual command constants as part of a collection (possibly a Map against the Character key), which can be tested to see if contains a matching command.

Categories

Resources