How to compare more than two strings? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
if ((A!= null || A!= "") && (B!= null || B!= "")
&& (C!= null || C!= "")elseif...elseif...elseif...
How we can do this without if else condition or in minimum code ??

Say you have a List<String> and any number could be null or empty you can do
List<String> values = ...
List<String> good = values.stream()
.filter(s -> s != null && !s.isEmpty())
.collect(Collectors.toList());
Instead of having lots of variables, you are better off having an appropriate collection.

I would group all the strings in a stream and then apply the same logic to each element:
Stream<String> strings = Stream.of(a, b, c, ...);
if (strings.allMatch(s -> s != null || s != "")) {
//
}
Note: I've changed variable names to a, b, c, etc., since Java conventions establish that variable names should be lowerCamelCase.

Related

opposite of if condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to rewrite my if else statement so I skip the //do nothing part but I can't get my around to find the opposite of the if statement.
someone please help?!
if (decision.equals("repay")){
String riskClass = null;
if (doc.hasItem("riskclass")){
riskClass = doc.getItemValueString("riskclass");
}
if ( (null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay")) ){
//do nothing
} else{
//do something
}
}
You can simply invert the condition. Try the snippet below.
if ( !((null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay"))) ){
//do something
}
Another way to invert the check is invert individual conditions and replace or's with and's:
if ( (null != riskClass) && (!riskClass.equals("")) && (!riskClass.equals("repay")) ){
Another point:
Avoid call equals on variables passing constants: the constant will never be null. So in this particular case would be better write:
if (!"repay".equals(riskClass) && !"".equals(riskClass))

For each Loop in Java 8 : What is the best way to write the following code in Java 8 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What is the best way to write the following code in Java 8 in terms of for loop and filtering.
boolean flag = true;
List<Feed> availableFeeds = data.getAvailableFeeds();
for (Feed feedElement : availableFeeds) {
String type = feedElement.getType();
if ("MatchState".equals(type)) {
flag = false;
break;
}
}
boolean flag = data.getAvailableFeeds()
.stream()
.map(Feed::getType)
.noneMatch("MatchState"::equals)
The first line creates a stream out of the list. The second one maps each Feed to type by calling getType. The last one returns true if there is no type that equals the string "MatchState".
You need filtering and a short break:
boolean flag = !availableFeeds.stream()
.map(Feed::getType)
.anyMatch(type -> "MatchState".equals(type));
or:
boolean flag = availableFeeds.stream()
.map(Feed::getType)
.allMatch(type -> !"MatchState".equals(type));
One way to do it is:
boolean flag = !data.getAvailableFeeds()
.stream()
.filter(Objects::nonNull)
.anyMatch(feed -> feed.getType().equals("MatchState"));
This example constructs a Stream<Feed> from the List<Feed> returned by getAvailableFeeds(), and then calls Stream<Feed>.anyMatch() which accepts a Predicate<Feed> as it's parameter, in this case the predicate is a feed where the feed is not null, and the feed's type returned by Feed.getType() equals "MatchState"

Java String comparison wrong outcome [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to get a Save button to enable/disable based on if the EditTexts actually change, but my string comparison is not working at all.
public void afterTextChanged(Editable editable) {
String newSet = editable.toString();
newSet = newSet.trim();
if(!newSet.equals(ip) || !newSet.equals(port) || !newSet.equals(username) || !newSet.equals(password)){
saveButton.setEnabled(true);
}else{
saveButton.setEnabled(false);
}
}
It keeps telling me the strings are different even though they aren't. Even when I print it out I get exactly the same String back.
Can anyone help me?
Thanks
Probably you want && instead of ||:
public void afterTextChanged(Editable editable) {
String newSet = editable.toString().trim();
saveButton.setEnabled(!newSet.equals(ip) &&
!newSet.equals(port) &&
!newSet.equals(username) &&
!newSet.equals(password));
}
enable saveButton if newSet is not a ip, port, username or password
You should write it like that, way easier to read :
if (!Arrays.asList(ip, port, username, password).contains(newSet))
{
saveButton.setEnabled(true);
}
else
{
saveButton.setEnabled(false);
}
Or :
saveButton.setEnabled(!Arrays.asList(ip, port, username, password).contains(newSet));
newSet can't be equal to all four of these Strings, unless all 4 are equal to each other. Therefore the condition will most likely return false.
If you require that newSet be equal to either one of those 4 Strings, the correct condition would be :
if(!(newSet.equals(ip) || newSet.equals(port) || newSet.equals(username) || newSet.equals(password)))
replace || with &&
if(!newSet.equals(ip) && !newSet.equals(port) && !newSet.equals(username) && !newSet.equals(password))
Reason :
OR(||) If any condition get satisfied it will enter inside the loop.
AND(&&) If all conditions get satisfied then and then only it will enter inside the loop.
In your case you need to satisfy all the conditions, that's why use and operator instead of or operator.

If statement based on what boolean method returns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to use this boolean method in my app. I want to use what it returns in an if statement so if it returns true I can do something. Something like
if(isNook() == True) {
//do something
}
I'm sure the answer is obvious but I couldn't find anything on how to do this.
Why don't you use:
if (isNook()) {
// do something
}
People are suggesting this:
if (isNook()) {
// do something
}
which is correct. However, we need to discuss your attempt. This was it:
if(isNook() == True) {
// do something
}
In Java, boolean variables are represented with true and false - NOT True and False - the values are case sensitive.
To contrast, for example, Python uses the values True and False
Your attempt should have been this:
if(isNook() == true) {
// do something
}
Try this:
boolean bool_result;
if(bool_result = isNook( )) {
// do stuff with bool_result here
}
result can actually be other data types (i.e. int/char/string) as long as it evaluates to true or false.

Java int 'through' int operator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to figure out how to do something like:
int test = 1;
int test1 = 10;
if (value = test (though) test1) {
}
I've looked at oracles java operators but could not figure out how to do it.
The construct should check if value is between test and test1.
Can anybody tell me how to do this in Java?
if (value >= test && value <= test1)
{
//doSomething
}
Java does not support chained inequalities, ie test <= value <= test1, so instead you can just use two boolean expressions, connected via the boolean and operator, to get a logically equivalent conditional.
You should try something like with logical and operator
if (value > test && value < test1) {
// do something
}
or add >= to add equals comparison too.
It looks like you are looking for range operator that is common in a lot of programming languages, Java not being one of them, but the condition that you are trying to impose on the range will always be the same. You don't need to check every value in the range, merely the endpoints since it is contiguous:
if( value > test && value < test1 ) {
// do something
}
There is no through op in Java. You can do it with a simple if :
if (value >= test && value <= test1) {
// your code
}
This post begged to be clarified.
If you are checking that value is between test1 and test2 then you need:
if(value >= test && value <= test1){
// do stuff
}
Note that you should remove the = signs if value should be strictly between test and test1.
However, if you are checking that value is one of multiple tests from test0 "through" test10 for instance, then pack those tests in a set and check if value is among them:
import java.util.*;
Set tests = new HashSet();
tests.add(test);
tests.add(test1); // similarly for as many as you need
if(tests.contains(value)){
// do stuff
}

Categories

Resources