This is my below mehod for Validation .
public boolean validateData(Bagform[] bagdata) {
boolean flag = false;
int length = bagdata.length;
if (length == 2) {
for (int i = 0; i < bagdata.length; i++) {
if (bagdata[i].getCallType() == null) {
flag = true;
}
}
}
else {
flag = true;
}
return flag;
}
In this basically i am checking that if the getCallType() is not null for any of the values in the array .
If its not null , then its a valid data , so i am setting the flag to true .
(Please see the code above )
But for me , i am getting the flag value still as false ( Even though the getCallype() is not null )
Please help me .
You're setting the flag to true if the call type is null. I suspect you want:
public boolean validateData(Bagform[] bagdata) {
boolean flag = true;
int length = bagdata.length;
if (length == 2) {
for (int i = 0; i < bagdata.length; i++) {
if (bagdata[i].getCallType() == null) {
flag = false;
}
}
}
return flag;
}
It's not clear why you're only performing this validation when there are exactly two entries in the array though. Unless that's really deliberate, I'd have written this has:
public boolean validateData(Bagform[] bagdata) {
for (Bagform form : bagdata) {
if (form.getCallType() == null) {
return false;
}
}
return true;
}
Related
In the following code I would expect equals() to return true, but it does not. What am I missing here?
SparseBooleanArray array_0 = new SparseBooleanArray();
array_0.put(0, true);
array_0.put(2, true);
SparseBooleanArray array_1 = new SparseBooleanArray();
array_1.put(0, true);
array_1.put(2, true);
boolean isEqual = array_0.equals(array_1); // is false instead of true
Looking at both array in the debugger, they seem the same to me (they have a different shadow$_monitor_ value, but I have no idea what that is supposed to be). The toString() method returns the same string for both as well.
I am trying to write a unit test for a function that converts an EnumSet to a SparseBooleanArray, but I can't create the same array manually to compare it with the function's return value.
Edit
I should also mention that hasCode() returns different values as well, which should not, based on the documentation.
Looking at the source code both the equals and hashCode methods are not implemented for SparseBooleanArray, SparseIntArray, SparseLongArray and SparseArray. I would say this is a critical missing feature and should be reported to Google.
Anyway, I use for quite some time these utility methods to solve this problem:
public static boolean equals(SparseArray arrayOne, SparseArray arrayTwo){
if(arrayOne == arrayTwo){
return true;
} else if(arrayOne.size() != arrayTwo.size()){
return false;
}
for(int i = 0; i < arrayOne.size(); i++){
if(arrayOne.keyAt(i) != arrayTwo.keyAt(i)){
return false;
}
final Object valueOne = arrayOne.valueAt(i);
final Object valueTwo = arrayTwo.valueAt(i);
if(valueOne != null && !valueOne.equals(valueTwo)){
return false;
} else if(valueTwo != null && !valueTwo.equals(valueOne)){
return false;
}
}
return true;
}
public static boolean equals(SparseBooleanArray arrayOne, SparseBooleanArray arrayTwo){
if(arrayOne == arrayTwo){
return true;
} else if(arrayOne.size() != arrayTwo.size()){
return false;
}
for(int i = 0; i < arrayOne.size(); i++){
if(arrayOne.keyAt(i) != arrayTwo.keyAt(i)){
return false;
} else if(arrayOne.valueAt(i) != arrayTwo.valueAt(i)){
return false;
}
}
return true;
}
public static boolean equals(SparseIntArray arrayOne, SparseIntArray arrayTwo){
if(arrayOne == arrayTwo){
return true;
} else if(arrayOne.size() != arrayTwo.size()){
return false;
}
for(int i = 0; i < arrayOne.size(); i++){
if(arrayOne.keyAt(i) != arrayTwo.keyAt(i)){
return false;
} else if(arrayOne.valueAt(i) != arrayTwo.valueAt(i)){
return false;
}
}
return true;
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean equals(SparseLongArray arrayOne, SparseLongArray arrayTwo){
if(arrayOne == arrayTwo){
return true;
} else if(arrayOne.size() != arrayTwo.size()){
return false;
}
for(int i = 0; i < arrayOne.size(); i++){
if(arrayOne.keyAt(i) != arrayTwo.keyAt(i)){
return false;
} else if(arrayOne.valueAt(i) != arrayTwo.valueAt(i)){
return false;
}
}
return true;
}
It is however also possible (as mentioned in the comments), and possibly better, to subclass the SparseArray class and override the equals and hashCode methods.
Disclaimer: I did not test the hashCode or equals implementation of the code provided below, please write some tests yourself to make sure it works correctly. #DontTrustTheInternet
public class SparseBooleanArray extends android.util.SparseBooleanArray {
#Override
public boolean equals(Object o) {
if(!(o instanceof SparseBooleanArray)){
return false;
} else if(this == o){
return true;
}
final SparseBooleanArray other = (SparseBooleanArray) o;
if(size() != other.size()){
return false;
}
for(int i = 0; i < size(); i++){
if(keyAt(i) != other.keyAt(i)){
return false;
} else if(valueAt(i) != other.valueAt(i)){
return false;
}
}
return true;
}
#Override
public int hashCode() {
int result = 17;
for(int i = 0; i < size(); i++){
result = 31 * result + keyAt(i);
result = 31 * result + (valueAt(i)?1:0);
}
return result;
}
}
This Method tests if the accountnumber is in the given array
public boolean containsAccount(int accountNumber) {
int i;
boolean ausgabe = false;
for (i = 0; i < allAccounts.length; i++) {
if (allAccounts[i].getAccountNumber() == accountNumber) {
ausgabe = true;
}
else if (i == length() - 1) {
ausgabe = false;
}
}
return ausgabe;
}
Expected to return true or false but it seems to be a neverending loop which retirns nothing.
Try this
public boolean containsAccount(int accountNumber) {
for (int i = 0; i < allAccounts.length; i++) {
if (allAccounts[i].getAccountNumber() == accountNumber) {
return true;
}
}
// If the execution flow reaches this line, then that
// means that the account 'id' does not exist in the array
return false;
}
Much cleaner, less variables & makes it easy to detect the real problem in your code. (Could be something with the allAccounts array, or the accountNumber)
Can this code to be simplifed using for?
if ((col[0] == null) && (col[1] == null) && (col[2] == null) && (col[3] == null) && (col[4] == null)){
//statement
}
You can use a Java 8 feature with Stream API:
boolean allNull = Arrays.stream(col).allMatch(Objects::isNull);
Use a boolean flag:
boolean areAllNull = true;
for (int i = 0; i < col.length; i ++) {
if (col[i] != null) {
areAllNull = false;
break;
}
}
if (areAllNull) {
//statement
}
If you want to limit only to certain positions in the array change col.length by a variable or constant marking the limit:
int numberOfPositions = 5;
for (int i = 0; i < numberOfPositions ; i ++)`
To check if X elements in an array are null, you cannot reduce the number of checks (X) unless you can short-circuit them. However, you can have a cleaner "if" statement if you package it in a method:
if (isAllNull(col, 0, 4)){
// do stuff
}
public boolean isAllNull(Object[] col, int start, int end){
for (int index=start;index<=end;index++){
if (col[index] !=null){
return false;
}
}
return true;
}
This will return false immediately when it finds one of the values not null.
I have the following code in a class "Shop":
public Item sellItem()
{
displayItems();
int indexID = Shop.getInput();
if (indexID <= -1 && indexID >= wares.length)
{
System.out.println("Null"); // For testing purposes
return null;
}
else
{
return wares[indexID];
}
}
And I'd like to know how to write an if statement checking if this method is returning null for my while loop in my main class:
int shopInput = scan.nextInt();
if(shopInput >= 1 && shopInput <= allShops.length)
{
boolean leaveShop = true;
while(leaveShop)
{
allShops[shopInput - 1].sellItem();
if(???????? == null);
{
System.out.println("still null"); // For testing purposes
leaveShop = false;
}
}
}
Item item = allShops[shopInput - 1].sellItem();
if (item == null) {
System.out.println("still null"); // For testing purposes
...
}
(note that I removed the ; from the end of the if line)
Use this to check for null.
if (allShops[shopInput - 1].sellItem() == null) {}
EDIT: removed unnecessary double negative
Let's say I have a boolean method that uses an if statement to check whether the return type should be true or false:
public boolean isValid() {
boolean check;
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
And now, I want to use this method as a parameter of an if statement in a different method:
if(isValid == true) // <-- this is where I'm not sure
//stop and go back to the beginning of the program
else
//continue on with the program
So basically what I'm asking is, how do I check what the return type of the boolean method within the parameters of an if statement is? Your answers are deeply appreciated.
Since it's a method, to call it you should use parens afterwards, so your code would then become:
if(isValid()) {
// something
} else {
//something else
}
public boolean isValid() {
int number = 5;
return number > 4;
}
if (isValid()) {
...
} else {
...
}
You should be able to just call the function within the IF condition so:
if (isValid()) {
}else {
}
Since isValid() returns a boolean the condition will be evaluated right away. I have heard it is better form to create a local var just before you test you condition.
boolean tempBoo = isValid();
if (tempBoo) {
}else {
}
- If statement accepts only boolean value.
public boolean isValid() {
boolean check = false; // always intialize the local variable
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
}
if(isValid()){
// Do something if its true
}else{
// Do something if its false
}
if (isValid()) {
// do something when the method returned true
} else {
// do something else when the method returned false
}
You can use :
if(isValid()){
//do something....
}
else
{
//do something....
}
public boolean isValid() {
boolean check;
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
How can you do this whole method without boolean check?
So how to get rid of.. check = true, check = false, return check stuff?