When using an if statement, for instance,
if((isInt(search1.nextToken()) == true) && search1.nextToken() != "x")
would the result returned by search1.nextToken() have different values? This is all wrapped in a while loop as well, and I'm trying to figure out what would happen.
Yes, it would have different values. Whenever you do nextToken it would read the next available token. I would suggest to try with a simple java program to understand better.
Related
The header is :
else if(nameOfWebsite.indexOf("http://") == 0 && nameOfWebsite.indexOf("www.") !== 7)
the last part of it, !== 7 how do I make it compile so it still works like how it is supposed to?
Primtive comparison is done with
o1 == o2 for equality
o1 != o2 for non-equality
Object comparison is done with
obj1.equals(obj1) for equality
!obj1.equals(obj1) for non-equality
=== and !== are used in JS
To make the above statement work, you will just need to replace the !== by != as this is the primitive values equality check in Java. Thus, your code will be like this:
else if(nameOfWebsite.indexOf("http://") == 0 && nameOfWebsite.indexOf("www.") != 7)
From your code statement, it looks like you are trying to validate and possibly extract the value of a site URL. I would suggest using Regex APIs to perform the same function above especially if you have optional www in the URL. The code would look like the following:
String pattern = "^(http://|https://)?(www.)?([a-z0-9]+)(.[a-z0-9]+)$";
if(nameOfWebsite.matches(pattern)){
// the rest of the code goes here
}
If you would like to extract snippets of the URL, then you should have a look at the Pattern class in Java.
Hope this helps.
I have a code that compares data but that data contains null values not blank spaces ("") they may/may not be same i guess but I tried comparing repetitive NULL values like
for(int i=0;i<length;i++)
{
String data=sample_data.getData();// contains about
1000 null rows
if(data.equals(null) || data.equals("") || data== null
||data.isEmpty())
System.out.print("No Data");
}
I could have used .contains but that wont work because it is in the loop.
None of the method makes it print the output as No Data I have tried using try catch as well because in order to make sure if theres any error found and was correct at the same time isEmpty() and .equals(null) were somewhere or the other throwing the exception as NULL but even after removing and simply using ==Null realizing the fact that data is itself null and comparing like null.equals wont work but in case of == I found that the error exception got changed to value as 1 thereafter the same error and I wasnt able to recognize this.
Its the sample here which I am showing I cant post the exact but the above makes quite clear. Is there any other way I could treat these NULL values.
Note: I dont want to use a loop to iterate data as well and each time comparing NULL values because already theres an outer loop for large set of values dont go by the sample code.
Restoring windows to factory settings will restore windows (as its name suggests); it won't spoil anything.
In this
String data=sample_data.getData();// contains about 1000 null rows
if(data.equals(null) || data.equals("") || data== null
you are apparently concerned that data may be null.
If indeed it is null, then the very first clause in your if statement will throw a null pointer exception... because you can't call the equals method on a null reference.
You want
if (data == null || data.isEmpty())
and that covers all the cases of nothing, except possibly the case where data is "some number of space characters", but I'll leave that for you.
In my code I want to check if the status of a Dog object is not that of 3 Enums:
if((dogList.get(i).getStatus()!= dogStatus.SLEEPING
|| dogList.get(i).getStatus()!= dogStatus.WALKING
||dogList.get(i).getStatus()!= dogStatus.EATING )){
//do something
}
Is this the correct way to combine the 3 || conditions? I ask as my program is not behaving as I expect.
You want
if (status != SLEEPING && status != WALKING...)
Note the '&&', since you want to check it's not WALKING and it's not SLEEPING and so on
The direct answer to your question is that you should use && not ||, as explained well in this answer. However, since you are using an enum, you can tidy this up by using switch.
switch (dogList.get(i).getStatus()) {
case SLEEPING:case EATING:case WALKING:
break;
default:
// write your code here.
}
(Technically this is not exactly the same as your (corrected) version as that executes the code if the result is null, whereas switch throws a NullPointerException).
I met a small problem when running a Java class that I wrote, though the design is pretty straightforward. I've created a JPanel, and I've added four JTextFields onto it and I've attached a button to this JPanel, too. Then, I've associated an ActionListener to this button being pressed. The code is like:
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (imageIdField.getText() == "" &&
captionField.getText() == "" &&
creditField.getText() == "" &&
titleField.getText()== "")
{
mediaXML = "";
results.clear();
results.put("error1", "more");
}
else
{ ....
}
}
The strange thing is after I've pressed the OK button, and I did input text in those four JTextFields, still it will fall in the IF branch as if I didn't input any text in any of these four fields.
I've been debugging this for a while, but no clue. Could anyone give me some hint like whether .getText() == "" is a valid way for testing no input?
Thanks in advance!
As has been mentioned, using == is not correct. For readability, try:
field.getText().isEmpty()
or
field.getText().trim().isEmpty()
Generally a bad idea to use == on Strings, or most other things. It checks that the objects are exactly the same instance, not that they have the same value. "" != new String("").
field.getText().equals("")
Or possibly better:
field.getText().isEmpty()
Use getText().equals("") instead of ==
Use == to check if it is the same object in memory, and .equals("YOUR STRING") to check if the content of the object is the same.
You should use .equals. Also, you might want to do something like this:
imageField.getText().trim().length() == 0 //The same for the others
or
imageField.getText().trim().isEmpty() //The same for the others
if you want to make sure that the user has actually written some characters instead of just white spaces.
== only checks whether the left hand side and the right hand side refer to the exact same instance of an object. And since "" translates to something like new String(""), it will always return false, if you compare it with a string that already exists.
If you want to compare whether two instances of a class have the same state you need to use equals(). In your case *.getText().equals(""). A more elegant method would to use the isEmpty() method of the String class.
I've got a problem that I'm rather confused about. I have the following lines of code in my android application:
System.out.println(CurrentNode.getNodeName().toString());
if (CurrentNode.getNodeName().toString() == "start") {
System.out.println("Yes it does!");
} else {
System.out.println("No it doesnt");
}
When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else statement and prints "No it doesn't".
I wondered if the name of the node might have some kind of non-printing character in it, so I've checked the length of the string coming from getNodeName() and it is 5 characters long, as you would expect.
Has anyone got any idea what's going on here?
Use String's equals method to compare Strings. The == operator will just compare object references.
if ( CurrentNode.getNodeName().toString().equals("start") ) {
...
Use CurrentNode.getNodeName().toString().equals("start").
In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object identity (Think memory addresses), not the content.
You need to use .equals
if ("start".equals(CurrentNode.getNodeName().toString()) { ... }