Using java "for each" and "continue" in a loop - java

I have a big method which looks like this :
for (Person person : persons) {
if (!person.isValid()) {
continue;
}
....
boolean isDeleted = deletePersonFromDB1(person);
if (!isDeleted) {
continue;
}
....
}
Basically I want to delete a list of persons from differents DB sources. If any operation fails, I want to continue to next person.
I would like to simplify like this and put my business logic inside a method :
for (Person person : persons) {
checkValidityAndDelete(person)
}
But unfortunately, I cannot use the word continue inside my method checkValidityAndDelete

Make your checkValidityAndDelete method return a boolean to indicate whether the person is both valid and was deleted:
private boolean checkValidityAndDelete(Person person) {
return person.isValid() && deletePersonFromDB1(person);
}
So, if checkValidityAndDelete returns false, you can then continue, if it returns true, you can proceed with the rest of your code logic. You can also achieve this without the use of continue if you wish to do so:
for (Person person : persons) {
if(checkValidityAndDelete(person)) { // If the person is valid,
// perform logic...
}
// if not valid, skip if, and continue to next person...
}

Another option if you're looking to pull everything in the loop out to another method is simply return~ing from it to trigger the method to stop.
public void checkValidity(final Person person) {
if (person.something) {
return;
// From the calling loop, this will act as a continue
// since the method call would stop, and so the next
// loop iteration would start.
}
// Do some more stuff
}

Related

Recursion method does not work to add element into arrayList

I am trying to create a recursion of an ArrayList(friends) to fill up another ArrayList (recommendations). However, the recursion method does not work. The question is as below:
How should we go about recommending new friends? We’ll take the approach of recommending all of our friends’ friends and our friends’ friends’ friends, and so on. This is a perfect opportunity to use recursion – we can create a getRecommendations method that takes a FacebookUser as an argument. The method should return an ArrayList that contains all of the friends of the FacebookUser that is passed into it plus the result of calling the same getRecommendations method on all of that FacebookUser’s friends. Be careful not to add anyone to the list of recommendations if they are already on it – that could lead to an infinite loop.
public ArrayList<FacebookUser> getRecommendations(FacebookUser user){
ArrayList<FacebookUser> recommendations = new ArrayList<FacebookUser>();
for (FacebookUser friend : user.getFriends()) {
getRecommendations(friend, recommendations);
}
if (!recommendations.isEmpty()) {
System.out.println("Recommended Friends");
System.out.println(recommendations);
}
return recommendations;
}
public void getRecommendations(FacebookUser friend, ArrayList<FacebookUser> recommendations) {
if (friend != null && friend.getFriends() != null && !friend.getFriends().isEmpty()) {
for (FacebookUser friendFriend : friend.getFriends()) {
if (!recommendations.contains(friendFriend)) {
recommendations.add(friendFriend);
System.out.println(recommendations); //test
getRecommendations(friendFriend, recommendations);
}
}
}
}
I expect the recursion under the first getRecommendations() method to work and brings it down to the second getRecommendations() method. However, it skipped the following part:
getRecommendations(friend, recommendations);

How to check a boolean method return value in java

I am pretty new to Java what I am trying to do may seem really strange but it is a matter of me understanding a little bit about how Java works more than actually accomplishing a set result.
If I have a boolean method for instance:
public class doThings
{
// imagine the intial value of this variable is set
// and or modified using getters and setters between the declaration
// and the following method
private boolean boolVar;
public boolean doSomething()
{
if(boolVar == true)
{
//does things and then returns true
return true;
}
else
{
return false;
}
}
}
And I want to call this method in another class like so...
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
}
}
How do I check (in the class testDoThings) to see if the method has returned true or returned false and print messages accordingly something like this...
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
if (doSomething() returns true) // yes I am aware this is not
//valid
{
// print a statment
}
else if (doSomething() returns false) // yes once again not valid
{
// print a different statement
}
}
}
I am aware that you could put these messages in the class containing the method but if I want different messages depending on where the method is called and what it is a called on then putting things in the original class method is not always going to work.
If I am completely off the track here please let me know, if someone can explain this to me though, that would be great!
You can try like this:
if (someObject.doSomething()) //So if your function returns true then the below statement will be executed
{
// print a statment
}
else //This will check for the false condition of your function
{
// print a different statement
}
You can try something like this:
if(someObject.doSomething()){
System.out.print("foo1");
}
else{
System.out.print("foo2");
}
Here's one way:
if(someObject.doSomething() == true){
System.out.print("foo1");
}
else{
System.out.print("foo2");
}
Generally you compare two things using == operator: if (x == y) ... so we have:
if ( someObject.doSomething() == true ) {
//statements
} else {
//statement for the case where result of method call is false
}
BTW instead of if(x == true) you can simply write if(x).
Conditional structures like if, while, do..., etc receive a boolean value, so it isn't necessary to put "boolVar == true". Just doing "if (boolVar)" is enough. As for your example in the doSomething method, just doing "return boolVar;" would do the work, without the need of any ifs, unless you pretend to do some more things on it.
To check a function return value works in the same way. I mean, variables have a value and functions also, the only difference is that variables hold a value while functions calculate or generate a value. So, in your code:
public class testDoThings {
public void check() {
doThings someObject = new doThings();
boolean flag = sameObject.doSomething();
if (flag) {
// print a statment
} else {
//If you want to check falsehood, !flag would do.
// Notice the "!" sign before
// the flag variable?
// It is a NOT sign, so
// NOT true = false
// NOT false = true
// The if will execute its code
// When the result value is true
// So when the function return val is
// false.
// print a different statement
}
}
}
I hope this explanation is enough clear.

Better way of returning from condition

Which way of returning from condition is better , Like the process1 and process 2 both does the same. But I want to know better way returning.
In both cases I don't want to enter inside of loop, I just want to return. I would like to know that, Is there any performance difference If I put return before control passes to end. I don't want Java Virtual Machine to check end of loop and returning from there. I thought If I put return Immediately when the condition not satisfied, then I could see minor performance difference and also code readability. Please suggest me the best way.
Let us consider the below scenarios.
Process1:
public Method()
{ //Method
Company company = new Company(); //Object
if (null != Address && null = Address.location()) //Condition
{
return company; //I want to return
}
for (Location location: Address.location())
{
//forloop
}
return company; //return
}
Process2:
public Method()
{
Company company = new Company();
if (null != Address && null != Address.location())
{
//enters loop
}
return company; // return
}
There will be some performance impact. Iterating complete objects from the for loop to verify the condition.
For example:
We can write like this.
if(condition is false){
return ;
else{
for(DataType ref: collection){
if(true){
return;// return from here, so that it will not iterate remaining elements.
}
}
}
ex 2:
if there is a logic after the if and that should not be executed, if the object is null.
if(object is null){
return ;
}
//Remaining logic here will not be executed, if the object is null. it's a good way of writing.
ex 3:
if there is no logic after the if and else, then directly return from the end of method.
if(object is null){
return
}else{
//process logic and return.
}
you can write something like this.
if(object is not null){
// return either from here.
}
return here is also fine...

Validating a function in a database

I'm not sure how to validate this so that if an id value isnt found i can say that the ID value doesn't exist. i have a GUI function with a database in which you enter a id (attribute) of someone and it returns their other information (name, surname, etc) but when an id is not found my program crashes and im not quite sure how to make so that i can use a JOption pane when an id is not found.
int id = Integer.parseInt(jTextField15.getText());
Person updatePerson = new Person();
for (Person person : personList)
{
if (person.getPersonid() == id)
{
updatePerson = person;
}
}
jTextField11.setText(updatePerson.getFirstname());
jTextField17.setText(updatePerson.getSurname());
jTextField12.setText(updatePerson.getPersontype());
jTextField16.setText(updatePerson.getGender());
jSpinner5.setValue(updatePerson.getDateofbirth());
The program seems to work but i am getting an exception in thread message after it cant find the id:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value
any help to explain what this is and how to fix it as well as being able print a message to the user would be very appreciated
I am assuming you mean the following:
what should I do in my GUI when I look up an element and it is not found.
There are 2 approaches you can take:
1: leave out the elements. This works well (usually) for html and other generated user interfaces. After your loop you check if the updatePerson == null, if so, you skip generating the html elements (or text fields, etc)
2: for GUI's you generally use a default value. This can be done in one of 2 ways: you use default values for each field, or you create a dummy object.
int id = Integer.parseInt(jTextField15.getText());
Person updatePerson = null;
for (Person person : personList)
{
if (person.getPersonid() == id)
{
updatePerson = person;
break; // with break: get first found, without break: get last found.
// this depends on whether it is possible to have a person show up more than once.
}
}
if (person == null) {
jTextField11.setText("");
jTextField11.setVisible(false); // setting fields to empty, in case logic
jTextField17.setText(""); // is using them or if someone
jTextField17.setVisible(false); // sets visible true before setting text
jTextField12.setText(""); // as you don't want to output wrong data
jTextField12.setVisible(false); // even for a flicker of a second
jTextField16.setText("");
jTextField16.setVisible(false);
jSpinner5.setValue(SOME_GLOBAL_DEFAULT_DATE); // ie. 0 or 1970-01-01
jSpinner5.setVisible(false);
} else {
jTextField11.setText(updatePerson.getFirstname());
jTextField17.setText(updatePerson.getSurname());
jTextField12.setText(updatePerson.getPersontype());
jTextField16.setText(updatePerson.getGender());
jSpinner5.setValue(updatePerson.getDateofbirth());
}
or you can do (leaving the fields, but empty)
int id = Integer.parseInt(jTextField15.getText());
Person updatePerson = new Person(); // default constructor initializes with empty of default values
for (Person person : personList)
{
if (person.getPersonid() == id)
{
updatePerson = person;
break; // with break: get first found, without break: get last found.
// this depends on whether it is possible to have a person show up more than once.
}
}
jTextField11.setText(updatePerson.getFirstname());
jTextField17.setText(updatePerson.getSurname());
jTextField12.setText(updatePerson.getPersontype());
jTextField16.setText(updatePerson.getGender());
jSpinner5.setValue(updatePerson.getDateofbirth());
While writing this I realise I would end up doing a third approach which is related to the first:
if (updatePerson == null) {
panelContainingFields.setVisible(false);
} else {
jTextField11.setText(updatePerson.getFirstname());
jTextField17.setText(updatePerson.getSurname());
jTextField12.setText(updatePerson.getPersontype());
jTextField16.setText(updatePerson.getGender());
jSpinner5.setValue(updatePerson.getDateofbirth());
panelContainingFields.setVisible(true);
}
Simply put all the fields you use in a panel, and hide the panel when there's no elements.

Searching an ArrayList

I currently have 3 classes, a main class containing a GUI, in which i'm calling this method, a customer class containing the data, and a customerList class which gathers the data from the customer class, puts it into an array list, and also contains the search arraylist method.
I'm trying to implement a search method which can be called from my main class on an action event handler. I'm having a few problems though.
Whenever I run the method, the " System.out.println(customer.returnFamilyName());" line always displays the first familyname in my arraylist.
Don't hesitate to ask for more information, I'm not sure how well i've explained this.
Here is my method:
public void searchCustomer(String familyName) {
int index = 0;
boolean found = false;
customer customer;
while(index < CustomerList.size() && !found) {
customer = CustomerList.get(index);
if(customer.returnFamilyName().equalsIgnoreCase(familyName)) {
found = true;
break;
}
if(found == true) {
;
}
System.out.println(customer.returnFamilyName());
index++;
return;
}
}
It's not clear from your question what the intended behaivor actually is. Besides that, what is this ?
if (found == true);
Presumably you meant :
if (found) {
System.out.println...
}
But what if the same last name occurs twice in your list? Also why aren't using a Map instead of a List? Lookup will go from being O(n) to O(1)
Drop the ; in if (found == true); because that reads as: if this condition is true, do notihng and use braces always:
if (found == true) {
System.out.println(customer.returnFamilyName());
}
Also, include the increment inside the while loop, otherwise you are not really iterating anything.
This code seems to work because your first element happens to coincide with the searched element, try with a different one and you'll end up in a infinite loop.
Try with a version like this:
public void searchCustomer( String familyName ) {
for ( customer current : CustomerList ) {
if ( current.returnFamilyName().equalsIgnoreCase( familyName )) {
System.out.println( current.returnFamilyName() );
break;
}
}
}
Some additional remarks:
In Java clases should start with uppercase, so the class name should be declared as Customer instead of customer and variables start with lowercase, hence CustomerList should be customerList. Methods may avoid the return part and be named with a get
Also, search methods should better return the found value instead of printing it, so your final version could look like this:
public Customer searchCustomer( String familyName ) {
for ( Customer current : customerList ) {
if ( current.getFamilyName().equalsIgnoreCase( familyName ) ) {
return current;
}
}
return null;
}
You never increment index.
The code should be:
public void searchCustomer(String familyName) {
for (customer customer : CustomerList) {
if (customer.returnFamilyName().equalsIgnoreCase(familyName)) {
System.out.println(customer.returnFamilyName());
break;
}
}
}
Also, the 'customer' class should be called 'Customer' as class names should start with a capital, 'returnFamilyName' should be 'getFamilyName' as accessor methods by convention are named 'get' + the field name and 'CustomerList' should be 'customerList' as field names are supposed to start with a lowercase letter.
I would suggest try this:
System.out.println(customer.returnFamilyName());
index++;
if(found == true) { return;}
Don't forget to increment the while loop or it has the potential to run indefinitely.
You can elect to use what is known as an "enhanced for-loop", which allows you to eschew the need to increment values over CustomerList entirely. You have an object customer, so we can use that as follows:
for (customer cus: CustomerList) {
if(cus.returnFamilyName().equalsIgnoreCase(familyName)) {
System.out.println(cus.returnFamilyName());
return;
}
}
If you elect to stick with your original code (which is fine), then observe the changes in your code below.
while(index < CustomerList.size()) {
customer = CustomerList.get(index);
if (customer.returnFamilyName().equalsIgnoreCase(familyName)) {
System.out.println(customer.returnFamilyName());
break;
} else {
index++;
}
}

Categories